Java Reference
In-Depth Information
// Create an offset datetime
OffsetDateTime odt2 = OffsetDateTime.of(2012, 5, 11,
18, 10, 30, 0,
offset);
System.out.println("An offset datetime: " + odt2);
// Get the local date and time from the offset datetime
LocalDate ld1 = odt1.toLocalDate();
LocalTime lt1 = odt1.toLocalTime();
System.out.println("Current Local Date: " + ld1);
System.out.println("Current Local Time: " + lt1);
// Get the instant from the offset datetime
Instant i1 = odt1.toInstant();
System.out.println("Current Instant: " + i1);
// Create an offset datetime from the instant
ZoneId usChicago = ZoneId.of("America/Chicago");
OffsetDateTime odt3 = OffsetDateTime.ofInstant(i1, usChicago);
System.out.println("Offset datetime from instant: " + odt3);
}
}
Current offset time: 08:43:35.074-06:00
An offset time: 16:40:28+05:30
Current offset datetime: 2014-01-06T08:43:35.121-06:00
An offset datetime: 2012-05-11T18:10:30+05:30
Current Local Date: 2014-01-06
Current Local Time: 08:43:35.121
Current Instant: 2014-01-06T14:43:35.121Z
Offset datetime from instant: 2014-01-06T08:43:35.121-06:00
Zoned Datetime
An instance of the ZonedDateTime class represents a datetime with time zone rules. The time zone rules include a
zone offset and rules for its variation because of Daylight Saving Time. There are no ZonedDate and ZonedTime ; they
do not make sense.
The relationships between a ZonedDateTime and a LocalDateTime can be represented as
ZonedDateTime = LocalDateTime + ZoneId
The following is an example of creating a ZonedDateTime from a LocalDateTime :
ZoneId usCentral = ZoneId.of("America/Chicago");
LocalDateTime ldt = LocalDateTime.of(2012, Month.MAY, 11, 7, 30);
ZonedDateTime zdt = ZonedDateTime.of(ldt, usCentral);
System.out.println(zdt);
 
Search WWH ::




Custom Search