56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */
62 package java.time.chrono;
63
64 import static java.time.chrono.JapaneseDate.MEIJI_6_ISODATE;
65 import static java.time.temporal.ChronoField.ERA;
66
67 import java.io.DataInput;
68 import java.io.DataOutput;
69 import java.io.IOException;
70 import java.io.InvalidObjectException;
71 import java.io.ObjectInputStream;
72 import java.io.ObjectStreamException;
73 import java.io.Serializable;
74 import java.time.DateTimeException;
75 import java.time.LocalDate;
76 import java.time.format.TextStyle;
77 import java.time.temporal.ChronoField;
78 import java.time.temporal.TemporalField;
79 import java.time.temporal.UnsupportedTemporalTypeException;
80 import java.time.temporal.ValueRange;
81 import java.util.Arrays;
82 import java.util.Locale;
83 import java.util.Objects;
84
85 import sun.util.calendar.CalendarDate;
86
87 /**
88 * An era in the Japanese Imperial calendar system.
89 * <p>
90 * This class defines the valid eras for the Japanese chronology.
91 * Japan introduced the Gregorian calendar starting with Meiji 6.
92 * Only Meiji and later eras are supported;
93 * dates before Meiji 6, January 1 are not supported.
94 *
95 * @implSpec
236 * @return an array of JapaneseEras
237 */
238 public static JapaneseEra[] values() {
239 return Arrays.copyOf(KNOWN_ERAS, KNOWN_ERAS.length);
240 }
241
242 /**
243 * {@inheritDoc}
244 *
245 * @param style {@inheritDoc}
246 * @param locale {@inheritDoc}
247 */
248 @Override
249 public String getDisplayName(TextStyle style, Locale locale) {
250 // If this JapaneseEra is a supplemental one, obtain the name from
251 // the era definition.
252 if (getValue() > N_ERA_CONSTANTS - ERA_OFFSET) {
253 Objects.requireNonNull(locale, "locale");
254 return style.asNormal() == TextStyle.NARROW ? getAbbreviation() : getName();
255 }
256 return Era.super.getDisplayName(style, locale);
257 }
258
259 //-----------------------------------------------------------------------
260 /**
261 * Obtains an instance of {@code JapaneseEra} from a date.
262 *
263 * @param date the date, not null
264 * @return the Era singleton, never null
265 */
266 static JapaneseEra from(LocalDate date) {
267 if (date.isBefore(MEIJI_6_ISODATE)) {
268 throw new DateTimeException("JapaneseDate before Meiji 6 are not supported");
269 }
270 for (int i = KNOWN_ERAS.length - 1; i > 0; i--) {
271 JapaneseEra era = KNOWN_ERAS[i];
272 if (date.compareTo(era.since) >= 0) {
273 return era;
274 }
275 }
276 return null;
|
56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */
62 package java.time.chrono;
63
64 import static java.time.chrono.JapaneseDate.MEIJI_6_ISODATE;
65 import static java.time.temporal.ChronoField.ERA;
66
67 import java.io.DataInput;
68 import java.io.DataOutput;
69 import java.io.IOException;
70 import java.io.InvalidObjectException;
71 import java.io.ObjectInputStream;
72 import java.io.ObjectStreamException;
73 import java.io.Serializable;
74 import java.time.DateTimeException;
75 import java.time.LocalDate;
76 import java.time.format.DateTimeFormatterBuilder;
77 import java.time.format.TextStyle;
78 import java.time.temporal.ChronoField;
79 import java.time.temporal.TemporalField;
80 import java.time.temporal.UnsupportedTemporalTypeException;
81 import java.time.temporal.ValueRange;
82 import java.util.Arrays;
83 import java.util.Locale;
84 import java.util.Objects;
85
86 import sun.util.calendar.CalendarDate;
87
88 /**
89 * An era in the Japanese Imperial calendar system.
90 * <p>
91 * This class defines the valid eras for the Japanese chronology.
92 * Japan introduced the Gregorian calendar starting with Meiji 6.
93 * Only Meiji and later eras are supported;
94 * dates before Meiji 6, January 1 are not supported.
95 *
96 * @implSpec
237 * @return an array of JapaneseEras
238 */
239 public static JapaneseEra[] values() {
240 return Arrays.copyOf(KNOWN_ERAS, KNOWN_ERAS.length);
241 }
242
243 /**
244 * {@inheritDoc}
245 *
246 * @param style {@inheritDoc}
247 * @param locale {@inheritDoc}
248 */
249 @Override
250 public String getDisplayName(TextStyle style, Locale locale) {
251 // If this JapaneseEra is a supplemental one, obtain the name from
252 // the era definition.
253 if (getValue() > N_ERA_CONSTANTS - ERA_OFFSET) {
254 Objects.requireNonNull(locale, "locale");
255 return style.asNormal() == TextStyle.NARROW ? getAbbreviation() : getName();
256 }
257
258 return new DateTimeFormatterBuilder()
259 .appendText(ERA, style)
260 .toFormatter(locale)
261 .withChronology(JapaneseChronology.INSTANCE)
262 .format(this == MEIJI ? MEIJI_6_ISODATE : since);
263 }
264
265 //-----------------------------------------------------------------------
266 /**
267 * Obtains an instance of {@code JapaneseEra} from a date.
268 *
269 * @param date the date, not null
270 * @return the Era singleton, never null
271 */
272 static JapaneseEra from(LocalDate date) {
273 if (date.isBefore(MEIJI_6_ISODATE)) {
274 throw new DateTimeException("JapaneseDate before Meiji 6 are not supported");
275 }
276 for (int i = KNOWN_ERAS.length - 1; i > 0; i--) {
277 JapaneseEra era = KNOWN_ERAS[i];
278 if (date.compareTo(era.since) >= 0) {
279 return era;
280 }
281 }
282 return null;
|