You can also do:
ZonedDateTime nowZdt = ZonedDateTime.now(zoneId);
ZonedDateTime todayAtMidnightZdt = nowZdt.with(LocalTime.MIDNIGHT);
I cannot think of an easier way to do this.
LocalDateTime vs ZonedDateTime
There is a (complicated) difference between LocalDateTime.now().atZone(zoneId)and ZonedDateTime.now(zoneId).
JVM, America/Sao_Paulo, (Europe/London). , 20 th 2017, - 17:56, 21:56.
:
LocalDateTime nowLdt = LocalDateTime.now();
LocalDateTime JVM . - ( 20 th 2017, 17:56):
2017-08-20T 17:56: 05,159
atZone, ZonedDateTime, :
ZoneId zoneId = ZoneId.of("Europe/London");
ZonedDateTime nowAtZone = nowLdt.atZone(zoneId);
nowAtZone :
2017-08-20T 17:56: 05,159 + 01: 00 [Europe/London]
(20 th 2017) (17:56) . , / . Milli:
System.out.println(nowAtZone.toInstant().toEpochMilli());
:
1503248165159
, LocalDateTime direclty, ZonedDateTime :
ZonedDateTime nowZdt = ZonedDateTime.now(zoneId);
, :
2017-08-20T 21:56: 05,170 + 01: 00 [Europe/London]
, ( 21:56). , , , . epochMilli:
System.out.println(nowZdt.toInstant().toEpochMilli());
:
1503262565170
, LocalDateTime ( , ). , ZonedDateTime.now(zoneId).
LocalDateTime.now().atZone() , , JVM JVM (- , TimeZone.setDefault()).
, DST ( ). , , (America/Sao_Paulo).
- DST 16 th 2016: 1 1 ( -03:00 -02:00). , 00:00 00:59 ( , 23: 59: 59.999999999 01:00). , :
ZoneId zone = ZoneId.of("America/Sao_Paulo");
// October 16th 2016 at midnight, DST started in Sao Paulo
LocalDateTime d = LocalDateTime.of(2016, 10, 16, 0, 0, 0, 0);
ZonedDateTime z = d.atZone(zone);
System.out.println(z);// adjusted to 2017-10-15T01:00-02:00[America/Sao_Paulo]
DST : 19 th 2017 1 , 23 18 th ( -02:00 -03:00). , 23:00 23:59 ( : -03:00 -02:00), , .
DST, withLaterOffsetAtOverlap() DST:
LocalDateTime d = LocalDateTime.of(2017, 2, 18, 23, 0, 0, 0);
ZonedDateTime beforeDST = d.atZone(zone);
System.out.println(beforeDST);
ZonedDateTime afterDST = beforeDST.withLaterOffsetAtOverlap();
System.out.println(afterDST);
, DST (-02:00 -03:00). epochMilli.
, with.