/** * Obtains an instance of {@code LocalTime} from a date-time object. * <p> * A {@code DateTimeAccessor} represents some form of date and time information. This factory converts the * arbitrary date-time object to an instance of {@code LocalTime}. * <p> * The conversion extracts the {@link ChronoField#NANO_OF_DAY nano-of-day} field. * * @param dateTime the date-time object to convert, not null * @return the local time, not null * @throws DateTimeException if unable to convert to a {@code LocalTime} */ public static LocalTime from(DateTimeAccessor dateTime) { if (dateTime instanceof LocalTime) { return (LocalTime) dateTime; } else if (dateTime instanceof ChronoLocalDateTime) { return ((ChronoLocalDateTime<?>) dateTime).getTime(); } else if (dateTime instanceof ZonedDateTime) { return ((ChronoZonedDateTime<?>) dateTime).getTime(); } // handle builder as a special case if (dateTime instanceof DateTimeBuilder) { DateTimeBuilder builder = (DateTimeBuilder) dateTime; LocalTime time = builder.extract(LocalTime.class); if (time != null) { return time; } } return ofNanoOfDay(dateTime.getLong(NANO_OF_DAY)); }
@SuppressWarnings("unused") @Test(dataProvider="samples") public void test_MinguoDate(MinguoDate minguoDate, LocalDate iso) { MinguoDate hd = minguoDate; ChronoLocalDateTime<MinguoDate> hdt = hd.atTime(LocalTime.NOON); ZoneOffset zo = ZoneOffset.ofHours(1); ChronoZonedDateTime<MinguoDate> hzdt = hdt.atZone(zo); hdt = hdt.plus(1, ChronoUnit.YEARS); hdt = hdt.plus(1, ChronoUnit.MONTHS); hdt = hdt.plus(1, ChronoUnit.DAYS); hdt = hdt.plus(1, ChronoUnit.HOURS); hdt = hdt.plus(1, ChronoUnit.MINUTES); hdt = hdt.plus(1, ChronoUnit.SECONDS); hdt = hdt.plus(1, ChronoUnit.NANOS); ChronoLocalDateTime<MinguoDate> a2 = hzdt.toLocalDateTime(); MinguoDate a3 = a2.toLocalDate(); MinguoDate a5 = hzdt.toLocalDate(); //System.out.printf(" d: %s, dt: %s; odt: %s; zodt: %s; a4: %s%n", date, hdt, hodt, hzdt, a5); }
@Test(dataProvider="calendars") public void test_badWithAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalAdjuster adjuster = new FixedAdjuster(cdt2); if (chrono != chrono2) { try { cdt.with(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + cdt + ", supplied: " + cdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.with(adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badPlusAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalAmount adjuster = new FixedAdjuster(cdt2); if (chrono != chrono2) { try { cdt.plus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + cdt + ", supplied: " + cdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.plus(adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date time"); } } }
@Test(dataProvider="calendars") public void test_badMinusAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalAmount adjuster = new FixedAdjuster(cdt2); if (chrono != chrono2) { try { cdt.minus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + cdt + ", supplied: " + cdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.minus(adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badPlusTemporalUnitChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalUnit adjuster = new FixedTemporalUnit(cdt2); if (chrono != chrono2) { try { cdt.plus(1, adjuster); Assert.fail("TemporalUnit.doPlus plus should have thrown a ClassCastException" + cdt + ", can not be cast to " + cdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.plus(1, adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badMinusTemporalUnitChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalUnit adjuster = new FixedTemporalUnit(cdt2); if (chrono != chrono2) { try { cdt.minus(1, adjuster); Assert.fail("TemporalUnit.doPlus minus should have thrown a ClassCastException" + cdt.getClass() + ", can not be cast to " + cdt2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.minus(1, adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badTemporalFieldChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalField adjuster = new FixedTemporalField(cdt2); if (chrono != chrono2) { try { cdt.with(adjuster, 1); Assert.fail("TemporalField doWith() should have thrown a ClassCastException" + cdt.getClass() + ", can not be cast to " + cdt2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.with(adjuster, 1); assertEquals(result, cdt2, "TemporalField doWith() failed to replace date"); } } }
@Override public int compareTo(ChronoLocalDateTime<?> other) { int cmp = getDate().compareTo(other.getDate()); if (cmp == 0) { cmp = getTime().compareTo(other.getTime()); if (cmp == 0) { cmp = getDate().getChrono().compareTo(other.getDate().getChrono()); } } return cmp; }
@Override public boolean isAfter(ChronoLocalDateTime<?> other) { long thisEpDay = getDate().toEpochDay(); long otherEpDay = other.getDate().toEpochDay(); return thisEpDay > otherEpDay || (thisEpDay == otherEpDay && getTime().toNanoOfDay() > other.getTime().toNanoOfDay()); }
@Override public boolean isBefore(ChronoLocalDateTime<?> other) { long thisEpDay = getDate().toEpochDay(); long otherEpDay = other.getDate().toEpochDay(); return thisEpDay < otherEpDay || (thisEpDay == otherEpDay && getTime().toNanoOfDay() < other.getTime().toNanoOfDay()); }
@Override public boolean isEqual(ChronoLocalDateTime<?> other) { // Do the time check first, it is cheaper than computing EPOCH day. return getTime().toNanoOfDay() == other.getTime().toNanoOfDay() && getDate().toEpochDay() == other.getDate().toEpochDay(); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof ChronoLocalDateTime) { return compareTo((ChronoLocalDateTime<?>) obj) == 0; } return false; }