/** * Obtains a {@code JapaneseDate} from a temporal object. * <p> * This obtains a date in the Japanese calendar system based on the specified temporal. * A {@code TemporalAccessor} represents an arbitrary set of date and time information, * which this factory converts to an instance of {@code JapaneseDate}. * <p> * The conversion typically uses the {@link ChronoField#EPOCH_DAY EPOCH_DAY} * field, which is standardized across calendar systems. * <p> * This method matches the signature of the functional interface {@link TemporalQuery} * allowing it to be used as a query via method reference, {@code JapaneseDate::from}. * * @param temporal the temporal object to convert, not null * @return the date in Japanese calendar system, not null * @throws DateTimeException if unable to convert to a {@code JapaneseDate} */ public static JapaneseDate from(TemporalAccessor temporal) { return JapaneseChronology.INSTANCE.date(temporal); }
public static JapaneseDate dateNow(JapaneseChronology instance) { return instance.dateNow(MockClock.systemDefaultZone()); }
private JapaneseDate resolveEYD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle, JapaneseEra era, int yoe) { if (resolverStyle == ResolverStyle.LENIENT) { int y = era.startDate().getYear() + yoe - 1; long days = Jdk8Methods.safeSubtract(fieldValues.remove(DAY_OF_YEAR), 1); return dateYearDay(y, 1).plus(days, DAYS); } int doy = range(DAY_OF_YEAR).checkValidIntValue(fieldValues.remove(DAY_OF_YEAR), DAY_OF_YEAR); return dateYearDay(era, yoe, doy); // smart is same as strict }
@Override public JapaneseDate resolveDate(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { if (fieldValues.containsKey(EPOCH_DAY)) { return dateEpochDay(fieldValues.remove(EPOCH_DAY)); updateResolveMap(fieldValues, MONTH_OF_YEAR, Jdk8Methods.floorMod(prolepticMonth, 12) + 1); updateResolveMap(fieldValues, YEAR, Jdk8Methods.floorDiv(prolepticMonth, 12)); JapaneseEra era = null; if (eraLong != null) { era = eraOf(range(ERA).checkValidIntValue(eraLong, ERA)); int yoe= range(YEAR_OF_ERA).checkValidIntValue(yoeLong, YEAR_OF_ERA); if (era == null && resolverStyle != ResolverStyle.STRICT && fieldValues.containsKey(YEAR) == false) { List<Era> eras = eras(); era = (JapaneseEra) eras.get(eras.size() - 1); fieldValues.remove(ERA); fieldValues.remove(YEAR_OF_ERA); return resolveEYMD(fieldValues, resolverStyle, era, yoe); return resolveEYD(fieldValues, resolverStyle, era, yoe); long months = Jdk8Methods.safeSubtract(fieldValues.remove(MONTH_OF_YEAR), 1); long days = Jdk8Methods.safeSubtract(fieldValues.remove(DAY_OF_MONTH), 1); return date(y, 1, 1).plusMonths(months).plusDays(days); } else { int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR); int dom = range(DAY_OF_MONTH).checkValidIntValue(fieldValues.remove(DAY_OF_MONTH), DAY_OF_MONTH); if (resolverStyle == ResolverStyle.SMART && dom > 28) {
long months = Jdk8Methods.safeSubtract(fieldValues.remove(MONTH_OF_YEAR), 1); long days = Jdk8Methods.safeSubtract(fieldValues.remove(DAY_OF_MONTH), 1); return date(y, 1, 1).plus(months, MONTHS).plus(days, DAYS); int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR); int dom = range(DAY_OF_MONTH).checkValidIntValue(fieldValues.remove(DAY_OF_MONTH), DAY_OF_MONTH); if (resolverStyle == ResolverStyle.SMART) { // previous valid if (yoe < 1) { dom = Math.min(dom, date(y, moy, 1).lengthOfMonth()); JapaneseDate jd = date(y, moy, dom); if (jd.getEra() != era) { return date(era, yoe, moy, dom);
@Override public ValueRange range(TemporalField field) { if (field == ChronoField.ERA) { return JapaneseChronology.INSTANCE.range(ChronoField.ERA); } return super.range(field); }
@Override // override for performance public int hashCode() { return getChronology().getId().hashCode() ^ isoDate.hashCode(); }
/** * Returns a copy of this date with the year altered. * <p> * This method changes the year of the date. * If the month-day is invalid for the year, then the previous valid day * will be selected instead. * <p> * This instance is immutable and unaffected by this method call. * * @param era the era to set in the result, not null * @param yearOfEra the year-of-era to set in the returned date * @return a {@code JapaneseDate} based on this date with the requested year, never null * @throws DateTimeException if {@code year} is invalid */ private JapaneseDate withYear(JapaneseEra era, int yearOfEra) { int year = JapaneseChronology.INSTANCE.prolepticYear(era, yearOfEra); return with(isoDate.withYear(year)); }
@Override public ChronoPeriod until(ChronoLocalDate endDate) { Period period = isoDate.until(endDate); return getChronology().period(period.getYears(), period.getMonths(), period.getDays()); }
@Override public ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (isSupported(field)) { ChronoField f = (ChronoField) field; switch (f) { case DAY_OF_YEAR: return actualRange(Calendar.DAY_OF_YEAR); case YEAR_OF_ERA: return actualRange(Calendar.YEAR); } return getChronology().range(f); } throw new UnsupportedTemporalTypeException("Unsupported field: " + field); } return field.rangeRefinedBy(this); }
@Override public JapaneseDate with(TemporalField field, long newValue) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; if (getLong(f) == newValue) { // validates unsupported fields return this; } switch (f) { case DAY_OF_YEAR: case YEAR_OF_ERA: case ERA: { int nvalue = getChronology().range(f).checkValidIntValue(newValue, f); switch (f) { case DAY_OF_YEAR: return with(isoDate.plusDays(nvalue - getDayOfYear())); case YEAR_OF_ERA: return this.withYear(nvalue); case ERA: { return this.withYear(JapaneseEra.of(nvalue), yearOfEra); } } } } return with(isoDate.with(field, newValue)); } return field.adjustInto(this, newValue); }
static ChronoLocalDate readExternal(DataInput in) throws IOException { int year = in.readInt(); int month = in.readByte(); int dayOfMonth = in.readByte(); return JapaneseChronology.INSTANCE.date(year, month, dayOfMonth); }
public static JapaneseDate dateNow(JapaneseChronology instance, ZoneId zone) { return instance.dateNow(MockClock.system(zone)); }
/** * Obtains a local date in Japanese calendar system from the * proleptic-year and day-of-year fields. * <p> * The day-of-year in this factory is expressed relative to the start of the proleptic year. * The Japanese proleptic year and day-of-year are the same as those in the ISO calendar system. * They are not reset when the era changes. * * @param prolepticYear the proleptic-year * @param dayOfYear the day-of-year * @return the Japanese local date, not null * @throws DateTimeException if unable to create the date */ @Override public JapaneseDate dateYearDay(int prolepticYear, int dayOfYear) { LocalDate date = LocalDate.ofYearDay(prolepticYear, dayOfYear); return date(prolepticYear, date.getMonthValue(), date.getDayOfMonth()); }