A ZonedDateTime
, , . ( ).
, , ZonedDateTime
, , (, , , , ).
- ZonedDateTime
, , LocalDateTime
:
public ZonedDateTime parseToZonedDateTime(String date, String dateFormat) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
ZonedDateTime zonedDateTime = null;
try {
zonedDateTime = ZonedDateTime.parse(date, formatter);
} catch (DateTimeException e) {
LocalDateTime dt = LocalDateTime.parse(date, formatter);
zonedDateTime = dt.atZone(ZoneId.systemDefault());
}
return zonedDateTime;
}
ZoneId.systemDefault()
, JVM , , runtime, , .
API IANA ( Region/City
, America/Sao_Paulo
Europe/Berlin
).
3- (, CST
PST
), .
( , ), ZoneId.getAvailableZoneIds()
.
, ZoneId.of("America/New_York")
( , ZoneId.getAvailableZoneIds()
, -) ZoneId.systemDefault()
.
parseBest()
method, ( TemporalQuery
" s), :
public ZonedDateTime parseToZonedDateTime(String date, String dateFormat) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
TemporalAccessor parsed = formatter.parseBest(date, ZonedDateTime::from, LocalDateTime::from);
if (parsed instanceof ZonedDateTime) {
return (ZonedDateTime) parsed;
}
if (parsed instanceof LocalDateTime) {
LocalDateTime dt = (LocalDateTime) parsed;
return dt.atZone(ZoneId.systemDefault());
}
return null;
}
ZonedDateTime::from
LocalDateTime::from
, ZonedDateTime
, , LocalDateTime
.
, , .
, ( , LocalDate
, LocalTime
, OffsetDateTime
.., from
, parseBest
- TemporalQuery
, , , ).
a LocalDateTime
ZonedDateTime
atZone()
, ().
, (America/Sao_Paulo
) , DST.
- 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
). DST, , .