Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.time equivalent of Joda-Time `withTimeAtStartOfDay`? (get first moment of the day)

In the Joda-Time library, the DateTime class offers a method withTimeAtStartOfDay to get the first moment of the day. You might think of that moment as "midnight". That first moment is usually the time 00:00:00.000 but not always.

Does the java.time package found in Java 8 and later have an equivalent feature?

like image 691
Basil Bourque Avatar asked May 18 '15 01:05

Basil Bourque


People also ask

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

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

How do you get start time and end of day?

ZonedDateTime zdtStart = zdt. toLocalDate(). atStartOfDay( zoneId ); Using Half-Open approach, get first moment of following day.

What is Joda time in Java?

Joda-Time is the most widely used date and time processing library, before the release of Java 8. Its purpose was to offer an intuitive API for processing date and time and also address the design issues that existed in the Java Date/Time API.

What is the replacement of Joda time Library in Java 8?

Correct Option: D. In java 8,we are asked to migrate to java. time (JSR-310) which is a core part of the JDK which replaces joda library project.


2 Answers

The equivalent is using a special method, atStartOfDay, in the class LocalDate:

ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime zdt = LocalDate.now(zoneId).atStartOfDay(zoneId);

Please also note that the equivalent of Joda-Time DateTime is not LocalDateTime, but ZonedDateTime. The zoneId parameter matters here. Concrete example for migration - see also timezone website for details about Daylight Saving Time (DST) transition in Brazil:

Joda-Time (old way)

DateTime dt = 
  new DateTime(2015, 10, 18, 12, 0, DateTimeZone.forID("America/Sao_Paulo"));
dt = dt.withTimeAtStartOfDay();
System.out.println(dt); // 2015-10-18T01:00:00.000-02:00

Note that this code would even throw an exception for midnight in first line with call to constructor.

java.time (new way)

ZoneId zoneId = ZoneId.of("America/Sao_Paulo");
ZonedDateTime zdt =
  ZonedDateTime.of(2015, 10, 18, 12, 0, 0, 0, zoneId);
zdt = zdt.toLocalDate().atStartOfDay(zoneId);
System.out.println(zdt); // 2015-10-18T01:00-02:00[America/Sao_Paulo]

The second program statement behaves differently than Joda-Time because it will not throw an exception but silently shifts the local time by the size of the gap in question, here one hour. This means, if you had chosen midnight, the result would be the same (namely at 1:00). If you had chosen 00:30, then the result would be 01:30. The example given above chooses noon as input.

To quote the doc for ZonedDateTime.of(…):

In most cases, there is only one valid offset for a local date-time. In the case of an overlap, when clocks are set back, there are two valid offsets. This method uses the earlier offset typically corresponding to "summer".

In the case of a gap, when clocks jump forward, there is no valid offset. Instead, the local date-time is adjusted to be later by the length of the gap. For a typical one hour daylight savings change, the local date-time will be moved one hour later into the offset typically corresponding to "summer".

A 100%-migration of all details like exception behaviour and applied DST transition strategies is not possible because both libraries are too different. But that is your guideline:

  • replace DateTime by ZonedDateTime
  • consider switching to LocalDate for intermediate calculations (see example)
  • use explicit references to the timezone and replace DateTimeZone by ZoneId
like image 133
Meno Hochschild Avatar answered Sep 22 '22 05:09

Meno Hochschild


You can use LocalDate to do this:

// or a similar factory method to get the date you want
LocalDateTime startOfDay = LocalDate.now().atStartOfDay();

Combines this date with the time of midnight to create a LocalDateTime at the start of this date. This returns a LocalDateTime formed from this date at the time of midnight, 00:00, at the start of this date.

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#atStartOfDay--

like image 22
Steve Chaloner Avatar answered Sep 20 '22 05:09

Steve Chaloner