@SuppressWarnings( "unchecked" ) @Override public <R extends Temporal> R adjustInto( R temporal, long newValue ) { int newVal = range.checkValidIntValue( newValue, this ); int oldYear = temporal.get( ChronoField.YEAR ); return (R) temporal.with( ChronoField.YEAR, (oldYear / years) * years + newVal ) .with( TemporalAdjusters.firstDayOfYear() ); }
/** * Get the number of weeks in the given month (period), with the leap year in the indicated month. * * @param month The month for which to get the count of weeks. * @param leapWeekInMonth The month in which the leap-week resides * @return The count of weeks in the given month, including any leap week. * @throws DateTimeException if the month isn't in the valid range of months. */ int getWeeksInMonth(int month, int leapWeekInMonth) { month = monthsInYearRange.checkValidIntValue(month, ChronoField.MONTH_OF_YEAR); leapWeekInMonth = (leapWeekInMonth == 0 ? 0 : monthsInYearRange.checkValidIntValue(leapWeekInMonth, ChronoField.MONTH_OF_YEAR)); return weeksInMonths[month - 1] + (month == leapWeekInMonth ? 1 : 0); }
/** * Get the number of weeks elapsed before the start of the month. * * @param month The month * @param leapWeekInMonth The month in which the leap-week resides * @return The number of weeks elapsed before the start of the month, including any leap week. * @throws DateTimeException if the month isn't in the valid range of months. */ int getWeeksAtStartOfMonth(int month, int leapWeekInMonth) { month = monthsInYearRange.checkValidIntValue(month, ChronoField.MONTH_OF_YEAR); leapWeekInMonth = (leapWeekInMonth == 0 ? 0 : monthsInYearRange.checkValidIntValue(leapWeekInMonth, ChronoField.MONTH_OF_YEAR)); return elapsedWeeks[month - 1] + (leapWeekInMonth != 0 && month > leapWeekInMonth ? 1 : 0); }
@Override public int prolepticYear(Era era, int yearOfEra) { if (!(era instanceof IsoEra)) { throw new ClassCastException("Invalid era: " + era); } YEAR_RANGE.checkValidIntValue(yearOfEra, ChronoField.YEAR_OF_ERA); return yearOfEra; }
@Override public int prolepticYear(Era era, int yearOfEra) { if (!(era instanceof IsoEra)) { throw new ClassCastException("Invalid era: " + era); } YEAR_RANGE.checkValidIntValue(yearOfEra, ChronoField.YEAR_OF_ERA); return yearOfEra; }
@Override public int prolepticYear(Era era, int yearOfEra) { if (!(era instanceof InternationalFixedEra)) { throw new ClassCastException("Invalid era: " + era); } YEAR_RANGE.checkValidIntValue(yearOfEra, ChronoField.YEAR_OF_ERA); return yearOfEra; }
@Override public int prolepticYear(Era era, int yearOfEra) { if (!DiscordianEra.YOLD.equals(era)) { throw new ClassCastException("Era must be DiscordianEra.YOLD"); } return YEAR_RANGE.checkValidIntValue(yearOfEra, ChronoField.YEAR_OF_ERA); }
/** * Checks that the specified value is valid and fits in an {@code int}. * <p> * This validates that the value is within the outer range of valid values * returned by {@link #range()}. * It also checks that all valid values are within the bounds of an {@code int}. * <p> * This method checks against the range of the field in the ISO-8601 calendar system. * This range may be incorrect for other calendar systems. * Use {@link Chronology#range(ChronoField)} to access the correct range * for a different calendar system. * * @param value the value to check * @return the value that was passed in */ public int checkValidIntValue(long value) { return range().checkValidIntValue(value, this); }
/** * Get the month from a count of elapsed weeks. * * @param weeksElapsed The weeks elapsed since the start of the year. * @param leapWeekInMonth The month in which the leap-week resides * @return the month * @throws DateTimeException if the month isn't in the valid range of months, * or the week isn't in the valid range. */ int getMonthFromElapsedWeeks(int weeksElapsed, int leapWeekInMonth) { if (weeksElapsed < 0 || weeksElapsed >= (leapWeekInMonth == 0 ? 52 : 53)) { throw new DateTimeException("Count of '" + elapsedWeeks.length + "' elapsed weeks not valid," + " should be in the range [0, " + (leapWeekInMonth == 0 ? 52 : 53) + ")"); } leapWeekInMonth = (leapWeekInMonth == 0 ? 0 : monthsInYearRange.checkValidIntValue(leapWeekInMonth, ChronoField.MONTH_OF_YEAR)); int month = Arrays.binarySearch(elapsedWeeks, weeksElapsed); // Binary search returns 0-indexed if found, negative - 1 for insert position if not. month = (month >= 0 ? month + 1 : 0 - month - 1); // Need to move to previous month if there was a leap week and in the first week. return leapWeekInMonth == 0 || month <= leapWeekInMonth || weeksElapsed > elapsedWeeks[month - 1] ? month : month - 1; } }
@Override InternationalFixedDate plusYears(long yearsToAdd) { if (yearsToAdd == 0) { return this; } int newYear = YEAR_RANGE.checkValidIntValue(Math.addExact(prolepticYear, yearsToAdd), ChronoField.YEAR); return resolvePreviousValid(newYear, month, day); }
@Override public int get(TemporalField field) { return range(field).checkValidIntValue(getLong(field), field); }
@Override public int get(TemporalField field) { if (field == ERA) { return getValue(); } return range(field).checkValidIntValue(getLong(field), field); }
@Override public int get(TemporalField field) { if (field == ERA) { return getValue(); } return range(field).checkValidIntValue(getLong(field), field); }
@Override public int get(TemporalField field) { if (field == ERA) { return getValue(); } return range(field).checkValidIntValue(getLong(field), field); }
@Override public int get(TemporalField field) { if (field == ERA) { return getValue(); } return range(field).checkValidIntValue(getLong(field), field); }
@Override public int get(TemporalField field) { if (field == ERA) { return getValue(); } return range(field).checkValidIntValue(getLong(field), field); }
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 }
@SuppressWarnings( "unchecked" ) @Override public <R extends Temporal> R adjustInto( R temporal, long newValue ) { int newVal = range.checkValidIntValue( newValue, this ); int oldYear = temporal.get( ChronoField.YEAR ); return (R) temporal.with( ChronoField.YEAR, (oldYear / years) * years + newVal ) .with( TemporalAdjusters.firstDayOfYear() ); }
@Override public int get(TemporalField field) { if (field instanceof ChronoField) { return (field.isTimeBased() ? time.get(field) : date.get(field)); } return range(field).checkValidIntValue(getLong(field), field); }
@SuppressWarnings("unchecked") @Override public <R extends Temporal> R adjustInto(R temporal, long newValue) { if (isSupportedBy(temporal) == false) { throw new UnsupportedTemporalTypeException("Unsupported field: WeekBasedYear"); } int newWby = range().checkValidIntValue(newValue, WEEK_BASED_YEAR); // strict check LocalDate date = LocalDate.from(temporal); int dow = date.get(DAY_OF_WEEK); int week = getWeek(date); if (week == 53 && getWeekRange(newWby) == 52) { week = 52; } LocalDate resolved = LocalDate.of(newWby, 1, 4); // 4th is guaranteed to be in week one int days = (dow - resolved.get(DAY_OF_WEEK)) + ((week - 1) * 7); resolved = resolved.plusDays(days); return (R) temporal.with(resolved); } };