@Override public long getLong(DateTimeField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case MONTH_OF_YEAR: return this.month; case EPOCH_MONTH: return getEpochMonth(); case YEAR_OF_ERA: return (this.year < 1 ? 1 - this.year : this.year); case YEAR: return this.year; case ERA: return (this.year < 1 ? 0 : 1); } throw new DateTimeException("Unsupported field: " + field.getName()); } return field.doGet(this); }
@Override public YearMonth with(DateTimeField field, long newValue) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; f.checkValidValue(newValue); switch (f) { case MONTH_OF_YEAR: return withMonth((int) newValue); case EPOCH_MONTH: return plusMonths(newValue - getLong(EPOCH_MONTH)); case YEAR_OF_ERA: return withYear((int) (this.year < 1 ? 1 - newValue : newValue)); case YEAR: return withYear((int) newValue); case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - this.year)); } throw new DateTimeException("Unsupported field: " + field.getName()); } return field.doWith(this, newValue); }
@Override public long getLong(DateTimeField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case NANO_OF_SECOND: return this.nanos; case MICRO_OF_SECOND: return this.nanos / 1000; case MILLI_OF_SECOND: return this.nanos / 1000000; case INSTANT_SECONDS: return this.seconds; } throw new DateTimeException("Unsupported field: " + field.getName()); } return field.doGet(this); }
@Override public DateTimeValueRange range(DateTimeField field) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; if (f.isDateField()) { switch (f) { case DAY_OF_MONTH: return DateTimeValueRange.of(1, lengthOfMonth()); case DAY_OF_YEAR: return DateTimeValueRange.of(1, lengthOfYear()); case ALIGNED_WEEK_OF_MONTH: return DateTimeValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5); case YEAR_OF_ERA: return (getYear() <= 0 ? DateTimeValueRange.of(1, MAX_YEAR + 1) : DateTimeValueRange.of(1, MAX_YEAR)); } return field.range(); } throw new DateTimeException("Unsupported field: " + field.getName()); } return field.doRange(this); }
@Override public ChronoZonedDateTime<C> with(DateTimeField field, long newValue) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; switch (f) { case INSTANT_SECONDS: return plus(newValue - toEpochSecond(), SECONDS); case OFFSET_SECONDS: { ZoneOffset offset = ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue)); return create(this.dateTime.toInstant(offset), this.zoneId); } } return ofBest(this.dateTime.with(field, newValue), this.zoneId, this.offset); } return getDate().getChrono().ensureChronoZonedDateTime(field.doWith(this, newValue)); }
@Override public long getLong(DateTimeField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case YEAR_OF_ERA: return (this.year < 1 ? 1 - this.year : this.year); case YEAR: return this.year; case ERA: return (this.year < 1 ? 0 : 1); } throw new DateTimeException("Unsupported field: " + field.getName()); } return field.doGet(this); }
@Override public Year with(DateTimeField field, long newValue) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; f.checkValidValue(newValue); switch (f) { case YEAR_OF_ERA: return Year.of((int) (this.year < 1 ? 1 - newValue : newValue)); case YEAR: return Year.of((int) newValue); case ERA: return (getLong(ERA) == newValue ? this : Year.of(1 - this.year)); } throw new DateTimeException("Unsupported field: " + field.getName()); } return field.doWith(this, newValue); }
@Override public DateTimeValueRange range(DateTimeField 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 getChrono().range(f); } throw new DateTimeException("Unsupported field: " + field.getName()); } return field.doRange(this); }
@Override public long getLong(DateTimeField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case YEAR_OF_ERA: return this.yearOfEra; case ERA: return this.era.getValue(); case DAY_OF_YEAR: { LocalGregorianCalendar.Date jdate = toPrivateJapaneseDate(this.isoDate); return JapaneseChrono.JCAL.getDayOfYear(jdate); } } // TODO: review other fields return this.isoDate.getLong(field); } return field.doGet(this); }
/** * Constructor. * * @param field the field to print, validated not null * @param width the field width, from 1 to 18 * @param baseValue the base value */ public ReducedPrinterParser(DateTimeField field, int width, int baseValue) { super(field, width, width, SignStyle.NOT_NEGATIVE); if (width < 1 || width > 18) { throw new IllegalArgumentException("The width must be from 1 to 18 inclusive but was " + width); } if (field.range().isValidValue(baseValue) == false) { throw new IllegalArgumentException("The base value must be within the range of the field"); } this.baseValue = baseValue; this.range = EXCEED_POINTS[width]; if ((((long) baseValue) + this.range) > Integer.MAX_VALUE) { throw new DateTimeException("Unable to add printer-parser as the range exceeds the capacity of an int"); } }
@Override public long getLong(DateTimeField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case YEAR_OF_ERA: { int prolepticYear = getProlepticYear(); return (prolepticYear >= 1 ? prolepticYear : 1 - prolepticYear); } case YEAR: return getProlepticYear(); case ERA: return (getProlepticYear() >= 1 ? 1 : 0); } return this.isoDate.getLong(field); } return field.doGet(this); }
private int parseSecs(String str) { if (str.equals("-")) { return 0; } int pos = 0; if (str.startsWith("-")) { pos = 1; } ParsePosition pp = new ParsePosition(pos); DateTimeBuilder bld = TIME_PARSER.parseToBuilder(str, pp); if (bld == null || pp.getErrorIndex() >= 0) { throw new IllegalArgumentException(str); } Map<DateTimeField, Long> parsed = bld.getFieldValueMap(); long hour = parsed.get(HOUR_OF_DAY); Long min = parsed.get(MINUTE_OF_HOUR); Long sec = parsed.get(SECOND_OF_MINUTE); int secs = (int) (hour * 60 * 60 + (min != null ? min : 0) * 60 + (sec != null ? sec : 0)); if (pos == 1) { secs = -secs; } return secs; }
@Override public boolean isSupported(DateTimeField field) { if (field instanceof ChronoField) { return ((ChronoField) field).isTimeField() || field == OFFSET_SECONDS; } return field != null && field.doIsSupported(this); }
@Override public DateTimeValueRange range(DateTimeField field) { if (field instanceof ChronoField) { if (field == OFFSET_SECONDS) { return field.range(); } return this.time.range(field); } return field.doRange(this); }
@Override public long getLong(DateTimeField field) { if (field instanceof ChronoField) { if (field == OFFSET_SECONDS) { return getOffset().getTotalSeconds(); } return this.time.getLong(field); } return field.doGet(this); }
@Override public boolean isSupported(DateTimeField field) { if (field instanceof ChronoField) { return field == YEAR || field == MONTH_OF_YEAR || field == EPOCH_MONTH || field == YEAR_OF_ERA || field == ERA; } return field != null && field.doIsSupported(this); }
@Override public DateTimeValueRange range(DateTimeField field) { if (field == YEAR_OF_ERA) { return (getYear() <= 0 ? DateTimeValueRange.of(1, LocalDate.MAX_YEAR + 1) : DateTimeValueRange.of(1, LocalDate.MAX_YEAR)); } return super.range(field); }
@Override public boolean isSupported(DateTimeField field) { if (field instanceof ChronoField) { return field == INSTANT_SECONDS || field == NANO_OF_SECOND || field == MICRO_OF_SECOND || field == MILLI_OF_SECOND; } return field != null && field.doIsSupported(this); }
@Override public boolean isSupported(DateTimeField field) { if (field instanceof ChronoField) { return field == MONTH_OF_YEAR || field == DAY_OF_MONTH; } return field != null && field.doIsSupported(this); }
@Override public DateTimeValueRange range(DateTimeField field) { if (field == MONTH_OF_YEAR) { return field.range(); } else if (field == DAY_OF_MONTH) { return DateTimeValueRange.of(1, getMonth().minLength(), getMonth().maxLength()); } return super.range(field); }
@Override public long getLong(DateTimeField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { // alignedDOW and alignedWOM not supported because they cannot be set in with() case DAY_OF_MONTH: return this.day; case MONTH_OF_YEAR: return this.month; } throw new DateTimeException("Unsupported field: " + field.getName()); } return field.doGet(this); }
@Override public DateTimeValueRange range(DateTimeField field) { if (field instanceof ChronoField) { return this.dateTime.range(field); } return field.doRange(this); }
@Override public int get(DateTimeField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: throw new DateTimeException("Field too large for an int: " + field); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return this.dateTime.get(field); } return super.get(field); }
@Override public long getLong(DateTimeField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: return toEpochSecond(); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return this.dateTime.getLong(field); } return field.doGet(this); }
@Override public boolean isSupported(DateTimeField field) { if (field instanceof ChronoField) { return ((ChronoField) field).isTimeField(); } return field != null && field.doIsSupported(this); }
@Override public DateTimeValueRange range(DateTimeField field) { if (field instanceof ChronoField) { if (((ChronoField) field).isTimeField()) { return field.range(); } throw new DateTimeException("Unsupported field: " + field.getName()); } return field.doRange(this); }
@Override public int get(DateTimeField field) { if (field instanceof ChronoField) { return get0(field); } return super.get(field); }
@Override public long getLong(DateTimeField field) { if (field instanceof ChronoField) { if (field == NANO_OF_DAY) { return toNanoOfDay(); } if (field == MICRO_OF_DAY) { return toNanoOfDay() / 1000; } return get0(field); } return field.doGet(this); }
private int get0(DateTimeField field) { switch ((ChronoField) field) { case NANO_OF_SECOND: return this.nano; case NANO_OF_DAY: throw new DateTimeException("Field too large for an int: " + field); case MICRO_OF_SECOND: return this.nano / 1000; case MICRO_OF_DAY: throw new DateTimeException("Field too large for an int: " + field); case MILLI_OF_SECOND: return this.nano / 1000000; case MILLI_OF_DAY: return (int) (toNanoOfDay() / 1000000); case SECOND_OF_MINUTE: return this.second; case SECOND_OF_DAY: return toSecondOfDay(); case MINUTE_OF_HOUR: return this.minute; case MINUTE_OF_DAY: return this.hour * 60 + this.minute; case HOUR_OF_AMPM: return this.hour % 12; case CLOCK_HOUR_OF_AMPM: int ham = this.hour % 12; return (ham % 12 == 0 ? 12 : ham); case HOUR_OF_DAY: return this.hour; case CLOCK_HOUR_OF_DAY: return (this.hour == 0 ? 24 : this.hour); case AMPM_OF_DAY: return this.hour / 12; } throw new DateTimeException("Unsupported field: " + field.getName()); }
@Override public long getLong(DateTimeField field) { if (field instanceof ChronoField) { if (field == EPOCH_DAY) { return toEpochDay(); } if (field == EPOCH_MONTH) { return getEpochMonth(); } return get0(field); } return field.doGet(this); }
private int get0(DateTimeField field) { switch ((ChronoField) field) { case DAY_OF_WEEK: return getDayOfWeek().getValue(); case ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((this.day - 1) % 7) + 1; case ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((getDayOfYear() - 1) % 7) + 1; case DAY_OF_MONTH: return this.day; case DAY_OF_YEAR: return getDayOfYear(); case EPOCH_DAY: throw new DateTimeException("Field too large for an int: " + field); case ALIGNED_WEEK_OF_MONTH: return ((this.day - 1) / 7) + 1; case ALIGNED_WEEK_OF_YEAR: return ((getDayOfYear() - 1) / 7) + 1; case MONTH_OF_YEAR: return this.month; case EPOCH_MONTH: throw new DateTimeException("Field too large for an int: " + field); case YEAR_OF_ERA: return (this.year >= 1 ? this.year : 1 - this.year); case YEAR: return this.year; case ERA: return (this.year >= 1 ? 1 : 0); } throw new DateTimeException("Unsupported field: " + field.getName()); }
/** * Returns a copy of this date with the specified field altered. * <p> * This method returns a new date based on this date with a new value for the specified field. This can be * used to change any field, for example to set the year, month of day-of-month. * <p> * In some cases, changing the specified field can cause the resulting date to become invalid, such as * changing the month from January to February would make the day-of-month 31 invalid. In cases like this, * the field is responsible for resolving the date. Typically it will choose the previous valid date, which * would be the last valid day of February in this example. * <p> * This instance is immutable and unaffected by this method call. * * @param field the field to set in the result, not null * @param newValue the new value of the field in the result * @return a {@code LocalDate} based on this date with the specified field set, not null * @throws DateTimeException if the value is invalid */ @Override public LocalDate with(DateTimeField field, long newValue) { if (field instanceof ChronoField) { ChronoField f = (ChronoField) field; f.checkValidValue(newValue); switch (f) { case DAY_OF_WEEK: return plusDays(newValue - getDayOfWeek().getValue()); case ALIGNED_DAY_OF_WEEK_IN_MONTH: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_MONTH)); case ALIGNED_DAY_OF_WEEK_IN_YEAR: return plusDays(newValue - getLong(ALIGNED_DAY_OF_WEEK_IN_YEAR)); case DAY_OF_MONTH: return withDayOfMonth((int) newValue); case DAY_OF_YEAR: return withDayOfYear((int) newValue); case EPOCH_DAY: return LocalDate.ofEpochDay(newValue); case ALIGNED_WEEK_OF_MONTH: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_MONTH)); case ALIGNED_WEEK_OF_YEAR: return plusWeeks(newValue - getLong(ALIGNED_WEEK_OF_YEAR)); case MONTH_OF_YEAR: return withMonth((int) newValue); case EPOCH_MONTH: return plusMonths(newValue - getLong(EPOCH_MONTH)); case YEAR_OF_ERA: return withYear((int) (this.year >= 1 ? newValue : 1 - newValue)); case YEAR: return withYear((int) newValue); case ERA: return (getLong(ERA) == newValue ? this : withYear(1 - this.year)); } throw new DateTimeException("Unsupported field: " + field.getName()); } return field.doWith(this, newValue); }
@Override public DateTimeValueRange range(DateTimeField field) { if (field instanceof ChronoField) { if (field == INSTANT_SECONDS || field == OFFSET_SECONDS) { return field.range(); } return getDateTime().range(field); } return field.doRange(this); }
@Override public int get(DateTimeField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: throw new DateTimeException("Field too large for an int: " + field); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return getDateTime().get(field); } return super.get(field); }
@Override public long getLong(DateTimeField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: return toEpochSecond(); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } return getDateTime().getLong(field); } return field.doGet(this); }
@Override public boolean isSupported(DateTimeField field) { if (field instanceof ChronoField) { return ((ChronoField) field).isDateField(); } return field != null && field.doIsSupported(this); }