The ZonedDateTime is a stable representation for timezones, dates and times. Its instance contains a moment in a specific timezone. In order to synchronize with system time, need to set time in it regularly.
There are other representations for datetime in Java. Communication with them is possible through exporting-importing Instant object.
The ZonedDateTime contains LocalDateTime, ZoneId and the resolved ZoneOffset.
The ZoneId contains all the rules of the specific time zone.
The ZoneOffset is used when only time shift is declared but without other time zone rules. In case the ZoneId is defined, this is handled automatically.
The LocalDateTime is a datetime stamp without zone information, contains LocalDate and LocalTime.
LocalDate is a date stamp.
LocalTime is a time stamp. Precision is nanosecond.
All components and the ZonedDateTime are immutable and thread-safe.
For setting new time zone or datetime parameters, due to immutability, creating absolutely new instance or overwrite the original is needed. If needed, use data of the previous object. For changing all or many parameters, create absolutely new instance.
From converting local datetime to an Instant (zoned datetime) is complicated since time zone offsets are not defined by their geometrical position on Earth. Their can be gaps and overlaps. The conversion tries to solve any such problems, by using the LocalDateTime and the ZoneOffset components.
ZonedDateTime has no explicit constructors but needs initialization before use.
In order to use it for only local time mode, initialize it with local time zone and do not modify the zone later.
The Date class of Java can handle a date from 1970 (1970.01.01 equals with zero number), that is, not proper for handling and managing dates universally.
The Calendar class of Java is not stable for some operations, e.g. for day-of-week, first-day-of-year, last-day-of-year calculates wrong data.
The GregorianCalendar is an extending class of Calendar, follows the Gregorian calendar system. Theoretically, contains all data according to the calendar system. Still, carries the problems of Calendar.
Every time zone has an inner identifier. Such ID can be used with ZonedDateTime methods. In order to produce it, use the ZoneId class.
For methods, passed time zone Strings can be:
offset from Greenwich Mean Time (e.g. GMT+01:30); will not use Daylight Saving Time (DST) offset
offset from Universal Coordinated Time (e.g. UTM+01:30); will not use DST offset
offset from Universal Time (e.g. UT+01:30); will not use DST offset
regional time definition in continent/city format (e.g. America/Los_Angeles); will use DST offset
short mosaic letters (e.g. CET) of time zone; due to having the same mosaic words for different zones on Earth, use it only when the ID is unique; will use DST offset
Z for the absolute zeroth time zone; will not use DST offset
in case wrong time zone is given, exception raises
For checking if the given year-month-day numbers form a legal date, use the data with zeroed time numbers with a ZonedDateTime constructor: no exception means legality.
For leap year check, use the formula:
new GregorianCalendar().isLeapYear(int year)DateTimeFormatter is used to create datetime pattern for converting back and forth to String. It is immutable and thread-safe.
All 3 components have toString() methods which enables them to e.g. send them to console output.
All 3 components have now() methods for accessing current system clock data and parse() methods for composing date, time or datetime from given text (for this, a DateTimeFormatter is used as parsing pattern of given data).
EXAMPLE FOR GETTING CURRENT SYSTEM CLOCK DATETIME AND FORMATTING:
EXAMPLE FOR INITIALIZATION OF LOCAL TIME ZONE AND LOCAL DATETIME:
EXAMPLE FOR INITIALIZATION OF A TIME ZONE AND LOCAL DATETIME:
EXAMPLE FOR INITIALIZATION OF A LOCAL TIME ZONE AND A LOCAL DATETIME:
EXAMPLE FOR CHANGING TIME ZONE:
EXAMPLE FOR CHANGING DATETIME PARAMETERS:
EXAMPLE FOR RELATIVELY CHANGING DATE AND TIME:
The ZonedDateTime is used together with the Duration class for calculating differences between two ZonedDateTime where if the result is positive, the 2nd (the passed) is greater.
The TimeUnit is used for converting between time units.
EXAMPLE FOR DURATION CALCULATIONS:
EXAMPLE FOR TIME UNIT CALCULATIONS:
The WeekFields and Locale classes are used for getting week-of-year information.
EXAMPLE FOR GETTING DATA FROM ZONEDDATETIME: