@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 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); }
@DataProvider(name = "samples") Object[][] data_samples() { return new Object[][] { { ChronoField.EPOCH_DAY, JAN01_1970, 0L }, { JulianDayField.JULIAN_DAY, JAN01_1970, 2400001L + 40587L }, { JulianDayField.MODIFIED_JULIAN_DAY, JAN01_1970, 40587L }, { JulianDayField.RATA_DIE, JAN01_1970, 710347L + (40587L - 31771L) }, { ChronoField.EPOCH_DAY, DEC31_1969, -1L }, { JulianDayField.JULIAN_DAY, DEC31_1969, 2400001L + 40586L }, { JulianDayField.MODIFIED_JULIAN_DAY, DEC31_1969, 40586L }, { JulianDayField.RATA_DIE, DEC31_1969, 710347L + (40586L - 31771L) }, { ChronoField.EPOCH_DAY, NOV12_1945, (-24 * 365 - 6) - 31 - 30 + 11 }, { JulianDayField.JULIAN_DAY, NOV12_1945, 2431772L }, { JulianDayField.MODIFIED_JULIAN_DAY, NOV12_1945, 31771L }, { JulianDayField.RATA_DIE, NOV12_1945, 710347L }, { ChronoField.EPOCH_DAY, JAN01_0001, (-24 * 365 - 6) - 31 - 30 + 11 - 710346L }, { JulianDayField.JULIAN_DAY, JAN01_0001, 2431772L - 710346L }, { JulianDayField.MODIFIED_JULIAN_DAY, JAN01_0001, 31771L - 710346L }, { JulianDayField.RATA_DIE, JAN01_0001, 1 }, }; }
@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); }
@Test(groups = { "tck" }) public void test_getLong_DateTimeField() { ZonedDateTime test = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 12, 30, 40, 987654321), ZONE_0100); assertEquals(test.getLong(ChronoField.YEAR), 2008); assertEquals(test.getLong(ChronoField.MONTH_OF_YEAR), 6); assertEquals(test.getLong(ChronoField.DAY_OF_MONTH), 30); assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1); assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182); assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12); assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30); assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40); assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321); assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0); assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1); assertEquals(test.getLong(ChronoField.OFFSET_SECONDS), 3600); assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), test.toEpochSecond()); }
@Test(groups = { "tck" }) public void test_get_DateTimeField() { ZonedDateTime test = ZonedDateTime.of(LocalDateTime.of(2008, 6, 30, 12, 30, 40, 987654321), ZONE_0100); assertEquals(test.get(ChronoField.YEAR), 2008); assertEquals(test.get(ChronoField.MONTH_OF_YEAR), 6); assertEquals(test.get(ChronoField.DAY_OF_MONTH), 30); assertEquals(test.get(ChronoField.DAY_OF_WEEK), 1); assertEquals(test.get(ChronoField.DAY_OF_YEAR), 182); assertEquals(test.get(ChronoField.HOUR_OF_DAY), 12); assertEquals(test.get(ChronoField.MINUTE_OF_HOUR), 30); assertEquals(test.get(ChronoField.SECOND_OF_MINUTE), 40); assertEquals(test.get(ChronoField.NANO_OF_SECOND), 987654321); assertEquals(test.get(ChronoField.HOUR_OF_AMPM), 0); assertEquals(test.get(ChronoField.AMPM_OF_DAY), 1); assertEquals(test.get(ChronoField.OFFSET_SECONDS), 3600); }
@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); }
@Test(groups = { "tck" }) public void test_get_DateTimeField() { OffsetDateTime test = OffsetDateTime.of(2008, 6, 30, 12, 30, 40, 987654321, OFFSET_PONE); assertEquals(test.getLong(ChronoField.YEAR), 2008); assertEquals(test.getLong(ChronoField.MONTH_OF_YEAR), 6); assertEquals(test.getLong(ChronoField.DAY_OF_MONTH), 30); assertEquals(test.getLong(ChronoField.DAY_OF_WEEK), 1); assertEquals(test.getLong(ChronoField.DAY_OF_YEAR), 182); assertEquals(test.getLong(ChronoField.HOUR_OF_DAY), 12); assertEquals(test.getLong(ChronoField.MINUTE_OF_HOUR), 30); assertEquals(test.getLong(ChronoField.SECOND_OF_MINUTE), 40); assertEquals(test.getLong(ChronoField.NANO_OF_SECOND), 987654321); assertEquals(test.getLong(ChronoField.HOUR_OF_AMPM), 0); assertEquals(test.getLong(ChronoField.AMPM_OF_DAY), 1); assertEquals(test.getLong(ChronoField.INSTANT_SECONDS), test.toEpochSecond()); assertEquals(test.getLong(ChronoField.OFFSET_SECONDS), 3600); }
@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 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 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); }