Java Reference
In-Depth Information
Instant instant = Instant.now();
ZonedDateTime zdt3 = instant.atZone(romeZone);
Figure 12.1 illustrates the components of a ZonedDateTime to help you understand the
differences between LocaleDate, LocalTime, LocalDateTime, and ZoneId.
Figure 12.1. Making sense of a ZonedDateTime
You can also convert a LocalDateTime to an Instant by using a ZoneId:
LocalDateTime dateTime = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45);
Instant instantFromDateTime = dateTime.toInstant(romeZone);
Or you can do it the other way around:
Instant instant = Instant.now();
LocalDateTime timeFromInstant = LocalDateTime.ofInstant(instant, romeZone);
12.3.1. Fixed offset from UTC/Greenwich
Another common way to express a time zone is with a fixed offset from UTC/Greenwich. For
instance, you can use this notation to say that “New York is five hours behind London.” In cases
like this you can use the ZoneOffset class, a subclass of ZoneId that represents the difference
between a time and the zero meridian of Greenwich, London:
ZoneOffset newYorkOffset = ZoneOffset.of("-05:00");
The -05:00 offset indeed corresponds to the US Eastern Standard Time. Be aware that a
ZoneOffset defined in this way doesn't have any Daylight Saving Time management, and for this
reason it isn't suggested in the majority of cases. Because a ZoneOffset is also a ZoneId, you can
use it as shown in listing 12.13 . You can also create an OffsetDateTime, which represents a
date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system:
 
Search WWH ::




Custom Search