/** * 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)); }
@Override public int compareTo(ChronoZonedDateTime<?> other) { int cmp = Jdk7Methods.Long_compare(toEpochSecond(), other.toEpochSecond()); if (cmp == 0) { cmp = getTime().getNano() - other.getTime().getNano(); if (cmp == 0) { cmp = getDateTime().compareTo(other.getDateTime()); if (cmp == 0) { cmp = getZone().getId().compareTo(other.getZone().getId()); if (cmp == 0) { cmp = getDate().getChrono().compareTo(other.getDate().getChrono()); } } } } return cmp; }
@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); ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); TemporalAdjuster adjuster = new FixedAdjuster(czdt2); if (chrono != chrono2) { try { czdt.with(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + czdt + ", supplied: " + czdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { ChronoZonedDateTime<?> result = czdt.with(adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badPlusAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); TemporalAmount adjuster = new FixedAdjuster(czdt2); if (chrono != chrono2) { try { czdt.plus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + czdt + ", supplied: " + czdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.plus(adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date time"); } } }
@Test(dataProvider="calendars") public void test_badMinusAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); TemporalAmount adjuster = new FixedAdjuster(czdt2); if (chrono != chrono2) { try { czdt.minus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + czdt + ", supplied: " + czdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.minus(adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badPlusTemporalUnitChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); TemporalUnit adjuster = new FixedTemporalUnit(czdt2); if (chrono != chrono2) { try { czdt.plus(1, adjuster); Assert.fail("TemporalUnit.doPlus plus should have thrown a ClassCastException, " + czdt + " can not be cast to " + czdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.plus(1, adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badMinusTemporalUnitChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); TemporalUnit adjuster = new FixedTemporalUnit(czdt2); if (chrono != chrono2) { try { czdt.minus(1, adjuster); Assert.fail("TemporalUnit.doPlus minus should have thrown a ClassCastException, " + czdt.getClass() + " can not be cast to " + czdt2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.minus(1, adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badTemporalFieldChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); TemporalField adjuster = new FixedTemporalField(czdt2); if (chrono != chrono2) { try { czdt.with(adjuster, 1); Assert.fail("TemporalField doWith() should have thrown a ClassCastException, " + czdt.getClass() + " can not be cast to " + czdt2.getClass()); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.with(adjuster, 1); assertEquals(result, czdt2, "TemporalField doWith() failed to replace date"); } } }
private void testZoneId(Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal) { String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]"; printFmtStr(locale, fmtStr); String expected = toZoneIdStr(test(fmtStr, locale, null, cal)); test(fmtStr, locale, expected, zdt); // get a new cal with fixed tz Calendar cal0 = Calendar.getInstance(); cal0.setTimeInMillis(zdt.toInstant().toEpochMilli()); cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId())); expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0)); if (zdt instanceof ZonedDateTime) { OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); test(fmtStr, locale, expected, odt); test(fmtStr, locale, expected, odt.toOffsetTime()); } // datetime + zid fmtStr = "c:[%tc] c:[%1$Tc]"; printFmtStr(locale, fmtStr); expected = toZoneIdStr(test(fmtStr, locale, null, cal)); test(fmtStr, locale, expected, zdt); }
private void testZoneId(Locale locale, ChronoZonedDateTime<?> zdt, Calendar cal) { String fmtStr = "z:[%tz] z:[%1$Tz] Z:[%1$tZ] Z:[%1$TZ]"; printFmtStr(locale, fmtStr); String expected = test(fmtStr, locale, null, cal); test(fmtStr, locale, expected, zdt); // get a new cal with fixed tz Calendar cal0 = Calendar.getInstance(); cal0.setTimeInMillis(zdt.toInstant().toEpochMilli()); cal0.setTimeZone(TimeZone.getTimeZone("GMT" + zdt.getOffset().getId())); expected = toZoneOffsetStr(test(fmtStr, locale, null, cal0)); if (zdt instanceof ZonedDateTime) { OffsetDateTime odt = ((ZonedDateTime)zdt).toOffsetDateTime(); test(fmtStr, locale, expected, odt); test(fmtStr, locale, expected, odt.toOffsetTime()); } // datetime + zid fmtStr = "c:[%tc] c:[%1$Tc]"; printFmtStr(locale, fmtStr); expected = test(fmtStr, locale, null, cal); test(fmtStr, locale, expected, zdt); }