Is there any way to represent a Date (without a time) that is associated with a time zone? The only representation of date without time I was able to find is LocalDate which I guess doesn't allow you to specify a zone.
ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.
You can convert ZonedDateTime to an instant, which you can use directly with Date. No, it will be the current Date on your zone system default.
A ZonedDateTime represents a date-time with a time offset and/or a time zone in the ISO-8601 calendar system. On its own, ZonedDateTime only supports specifying time offsets such as UTC or UTC+02:00 , plus the SYSTEM time zone ID.
Short answer, there is no such type in Java-8.
The combination of a calendar date and a time zone is questionable, at least to say. Reason is that time zones are intended to operate on date AND time. Since time zones are responsible for translating between global instants/moments and local timestamps (including date AND time) a combination of just a date and a zone is simply not complete to enable such transformations.
The only similar type I am aware of is the type
xs:date
in XML-Schema which explicitly allows an extra offset (which is less than a time zone because it does not store daylight saving rules or historical offsets). In my opinion the W3C-consortium has rather introduced this type for symmetry reasons not for real time-tasks. JSR-310 (which introduced the java.time-package into Java-8) originally intended to offer a similar type called OffsetDate
, see also this page. It was removed however when Java-8 was finished for release.
Of course, you can write a simple class yourself which holds two state members of types LocalDate
and ZoneId
(but what is your use-case???). For XML, I would rather choose LocalDate
and ZonalOffset
.
Maybe you can use the class ZonedDateTime:
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
LocalDate localDate = zonedDateTime.toLocalDate(); // gets you the date without time
ZoneId zoneId = zonedDateTime.getZone(); // gets you the timezone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With