DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder(); dtfb.append(DateTimeFormatter.ISO_LOCAL_DATE); dtfb.appendLiteral(' '); dtfb.append(DateTimeFormatter.ISO_LOCAL_TIME); DateTimeFormatter dtf = dtfb.toFormatter(); ZoneId shanghai = ZoneId.of("Asia/Shanghai"); String str3 = "1927-12-31 23:54:07"; String str4 = "1927-12-31 23:54:08"; ZonedDateTime zdt3 = LocalDateTime.parse(str3, dtf).atZone(shanghai); ZonedDateTime zdt4 = LocalDateTime.parse(str4, dtf).atZone(shanghai); Duration durationAtEarlierOffset = Duration.between(zdt3.withEarlierOffsetAtOverlap(), zdt4.withEarlierOffsetAtOverlap()); Duration durationAtLaterOffset = Duration.between(zdt3.withLaterOffsetAtOverlap(), zdt4.withLaterOffsetAtOverlap());
public ZonedDateTime withLaterOffsetAtOverlap() { return dt.withLaterOffsetAtOverlap(); }
public ZonedDateTime withLaterOffsetAtOverlap() { return dt.withLaterOffsetAtOverlap(); }
public ZonedDateTime withLaterOffsetAtOverlap() { return dt.withLaterOffsetAtOverlap(); }
@Test public void testZonedDateTime() throws Exception { // CET DST rolls back at 2011-10-30 2:59:59 (+02) to 2011-10-30 2:00:00 (+01) // Credit: user leonbloy at http://stackoverflow.com/a/18794412/6692043 LocalDateTime localDateTime = LocalDateTime.of( 2011, 10, 30, 2, 50, 0, 0 ); ZonedDateTime value = localDateTime.atZone( ZoneId.of( "CET" ) ).withLaterOffsetAtOverlap(); Sample sample = new Sample( 1L, "ZonedDateTime example" ); sample.zonedDateTime = value; assertThatFieldIsIndexed( "zonedDateTime", value, sample ); }
@Test public void testZonedDateTimeNanoseconds() throws Exception { // CET DST rolls back at 2011-10-30 2:59:59 (+02) to 2011-10-30 2:00:00 (+01) // Credit: user leonbloy at http://stackoverflow.com/a/18794412/6692043 LocalDateTime localDateTime = LocalDateTime.of( 2011, 10, 30, 2, 50, 0, 7 ); ZonedDateTime value = localDateTime.atZone( ZoneId.of( "CET" ) ).withLaterOffsetAtOverlap(); Sample sample = new Sample( 1L, "ZonedDateTime example" ); sample.zonedDateTime = value; // Elasticsearch only has millisecond-precision, so the "fields" value is missing the nanoseconds // Also, the "fields" attribute only ever contains UTC date/times assertThatFieldIsFormatted( sample, "zonedDateTime", "2011-10-30T02:50:00.000000007+01:00[CET]", "2011-10-30T01:50:00.000+00:00[UTC]" ); }