Migration from Joda time to Java 8 DateTime

I could not find an alternative toDateTimeAtStartOfTheDay. for instance DateTime.now().toLocalDate().toDateTimeAtStartOfDay().plusHours(10)

How would I write the above code in the Java 8 DateTime library? The closest I came to ZonedDateTime.now().toLocalDate().atStartOfDay()is that just prints 2015-07-21T00:00.
I need something like2015-07-21T00:00:00.000-04:00

+4
source share
2 answers

If you need time as a formatted string, and you always like to get 10 hours today, then do not worry, calculating this time manually and writing it to the format template:

DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T10:00:00.000'XXX");

The meaning of each letter can be found here: JavaDocDateTimeFormatter . 'T10:00:00.000'is a fixed string and will not be parsed, just "added" to the returned string.

:

 ZonedDateTime.now().format(format);

:

2015-07-21T10: 00: 00.000-04: 00

+3

:

 LocalDate.now().atStartOfDay().atZone(ZoneId.systemDefault());
+3

Source: https://habr.com/ru/post/1598933/


All Articles