Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 date-time: get start of day from ZonedDateTime

Is there any difference between these:

zonedDateTime.truncatedTo(ChronoUnit.DAYS);  zonedDateTime.toLocalDate().atStartOfDay(zonedDateTime.getZone()); 

Any reason to prefer one against the other?

Thanks

like image 844
Nazaret K. Avatar asked Mar 19 '15 11:03

Nazaret K.


People also ask

How do you get day from ZonedDateTime?

The getDayOfWeek() method of a ZonedDateTime class is used to get the day-of-week field from this ZonedDateTime, which is an enum DayOfWeek. Additional information can be obtained from the DayOfWeek. This includes textual names of the values. Parameters: This method does not take any parameters.

How do I get the start of a date in Java?

LocalDateTime startOfDay = LocalDateTime. of(localDate, LocalTime. MIDNIGHT);

What is a ZonedDateTime in the Java 8 date and time API?

ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.

Is ZonedDateTime a UTC?

A ZonedDateTime represents a date-time with a time offset and/or a time zone in the ISO-8601 calendar system. On its own, ZonedDateTime only supports specifying time offsets such as UTC or UTC+02:00 , plus the SYSTEM time zone ID.


2 Answers

Updated for sake of correction:

In most cases yes the same, see following example for Brazil when switching from winter to summer time:

ZonedDateTime zdt =    ZonedDateTime.of(2015, 10, 18, 0, 30, 0, 0,      ZoneId.of("America/Sao_Paulo")); // switch to summer time ZonedDateTime zdt1 = zdt.truncatedTo(ChronoUnit.DAYS); ZonedDateTime zdt2 = zdt.toLocalDate().atStartOfDay(zdt.getZone());  System.out.println(zdt); // 2015-10-18T01:30-02:00[America/Sao_Paulo] System.out.println(zdt1); // 2015-10-18T01:00-02:00[America/Sao_Paulo] System.out.println(zdt2); // 2015-10-18T01:00-02:00[America/Sao_Paulo] 

Truncating happens on the local timeline. If you choose DAYS then you opt for midnight. According to javadoc the truncate()-method finally converts back to the new ZonedDateTime and shifts the time forward by the size of the gap (1 hour).

Converting the zdt first to LocalDate (cutting off the time part) and then looking for its ZonedDateTime-part in given timezone is effectively the same for this situation.

However, for the reverse case of switching back from summer time to winter time there is one exception (thanks very much to @Austin who gave a counter example). The problem is during overlap when to decide which offset to be used. Usually the class ZonedDateTime is designed/specified to use the previous offset, see also this excerpt from Javadoc:

For Overlaps, the general strategy is that if the local date-time falls in the middle of an Overlap, then the previous offset will be retained. If there is no previous offset, or the previous offset is invalid, then the earlier offset is used, typically "summer" time.

If the class ZonedDateTime would consequently follow its own specification then both procedures would still be equivalent meaning:

zdt.truncatedTo(ChronoUnit.DAYS); 

should be equivalent to

zdt.toLocalDate().atStartOfDay().atZone(zdt.getZone()).withEarlierOffsetAtOverlap(); 

But the real behaviour according to the example of @Austin and confirmed by me in own testing is:

zdt.toLocalDate().atStartOfDay().atZone(zdt.getZone()).withLaterOffsetAtOverlap(); 

Looks like a hidden inconsistency in the class ZonedDateTime, mildly spoken. If you ask me which method to be preferred then I would rather advocate the second method although it is much longer and requires more keystrokes. But it has the big advantage to be more transparent about what it does. Another reason to prefer the second approach is:

It really obtains the FIRST instant at which the local time is equal to the start of day. Otherwise, when using first method, you have to write:

zdt.truncatedTo(ChronoUnit.DAYS).withEarlierOffsetAtOverlap(); 
like image 96
Meno Hochschild Avatar answered Sep 17 '22 18:09

Meno Hochschild


They are slightly different. According to the javadocs, truncatedTo() will try to preserve the time zone in the case of overlap, but atStartOfDay() will find the first occurrence of midnight.

For example, Cuba reverts daylight savings at 1am, falling back to 12am. If you begin with a time after that transition, atStartOfDay() will return the first occurence of 12am, while truncatedTo() will return the second occurence.

ZonedDateTime zdt = ZonedDateTime.of(2016, 11, 6, 2, 0, 0, 0, ZoneId.of("America/Havana")); ZonedDateTime zdt1 = zdt.truncatedTo(ChronoUnit.DAYS); ZonedDateTime zdt2 = zdt.toLocalDate().atStartOfDay(zdt.getZone()); ZonedDateTime zdt3 = zdt.with(LocalTime.MIN);  System.out.println(zdt);  // 2016-11-06T02:00-05:00[America/Havana] System.out.println(zdt1); // 2016-11-06T00:00-05:00[America/Havana] System.out.println(zdt2); // 2016-11-06T00:00-04:00[America/Havana] System.out.println(zdt3); // 2016-11-06T00:00-05:00[America/Havana] 
like image 37
Austin Avatar answered Sep 16 '22 18:09

Austin