DateTime and TimeZones in Java

Ranmal Dewage
5 min readMay 16, 2021
Source (https://c.tadst.com/gfx/1200x630/meeting-planner.png?1)

In the versions before Java 8, we had Date and Calendar APIs to handle date and time. To overcome the shortcomings of the older java util Date and java util Calendar, Java 8 introduced new APIs for Date and Time. Some of the issues of the existing older java Date and Calender are mentioned below.

  • Thread safety: The older Date and Calendar classes are not thread-safe. Therefore, developers have to handle the concurrency issues. But the new Date and Time APIs introduced in Java 8 are immutable and thread-safe.
  • API design and ease of understanding: Older Date and Calendar APIs are poorly designed. The new Date/Time API is ISO-centric and follows consistent models for the date, time, duration, and periods.
  • ZonedDate and Time: In the older version, we had to write additional logic to handle time in different time zones. But in the new version, time zone differences can be handled by the Local and ZonedDate/Time APIs.

LocalDate, LocalTime and LocalDateTime

We use these classes when time zones are not needed to be mention in our developing context. As the name indicates it represents the date and time from the observer’s context.

LocalDate

LocalDate represents in ISO format without time (YYYY-MM-dd).

Creation of LocalDate Instance from Current Date using the System Clock

Apart from that, we can pass arguments and create LocalDate instances as below.

Passing Argument to Create an Instance of the LocalDate

There are several methods available for the manipulation of instances of the LocalDate class. I have mentioned two of the most used methods. You can check the Java documentation to get more information about them.

Using methods to get the Day of the Week and the Day of the Month

LocalTime

LocalTime represents the Time without a date. We can also use the parse and of methods or use the system clock (now) to generate LocalTime. You can see some of the commonly used methods with LocalTime below;

Creation of LocalTime Instance from Current Time using the System Clock
Passing Argument to Create an Instance of the LocalTime
Get the Hour from the LocalTime instance
Comparison of two LocalTime instances
Get the maximum amount of time a LocalTime instance can have (23:59:59.99)

LocalDateTime

LocalDateTime is used to represent a combination of date and time. The most commonly used class developers go for when they need a combination of date and time. Can use the parse and of methods or use the system clock (now) with LocalDateTime to generate an instance of it.

Different ways to create an instance of LocalDateTime

ZonedDateTime API

If we need to deal with different time zones requirement, Java 8 provides a class called ZonedDateTime. The ZoneId is an identifier used to represent different zones. There are about 40 different time zones, and ZoneId is used to represent them.

Creation and Usage of ZoneDateTime and ZoneId instances

As mentioned above, firstly, we can create a ZoneId by passing a valid time zone into its constructor. After that, we can use that ZoneId with the previously created LocalDateTime for the creation of ZonedDateTime for tracing time zone details accurately.

Creation of OffsetDateTime using LocalDateTime and ZoneOffset

Apart from that, we can use LocaDateTime with OffsetDateTime to work with timezones. OffsetDateTime class tracks the offset from UTC/Greenwich, and its date and time fields are stored to a precision of nanoseconds. First, create an instance of LocalDateTime by passing arguments to the “of” method as mention above. Then create a ZoneOffset by specifying the offset amount from the UTC/Greenwich. After that, you can use that LocalDateTime with ZoneOffset for the creation of the OffsetDateTime instance. Now OffsetDateTime represent the date-time with an offset as 2015–02–20 06:30 +02:00.

Daylight Savings Time in Java

Daylight Saving Time (DST) is a practice of moving clocks forward during summer months and backward in the months of winter to leverage an additional hour of natural light (saving heating power, illumination power, enhancing the mood). We need to take this scenario into account when working with dates and times. As several countries adopt this technique.
We will see how the new Java Date/Time API handles DST according to different locations.

Creation of LocalDateTime and ZoneId and combine them to create a ZoneDateTime

As mentioned above, firstly, we will create an instance of LocalDateTime, and the initial output is 2018-03-25T01:55. It’s completely unaware of Zones and Offsets; that is why we need to convert it into a fully DST ZonedDateTime. After converting to ZonedDateTime, it will give an output like 2018-03-25T01:55+01:00[Europe/Rome]. You can see the ZonedDateTime included two trailing pieces of information like +01:00 is the ZoneOffset and [Europe/Rome] is the ZoneId.

Manually trigger DST shift by addition of 15 minutes to ZoneDateTime

After that, as shown above, let’s trigger the DST by the addition of 15 minutes to the ZonedDateTime since 2018-03-25 01:55 am, only five minutes away from the DST shift in Italy/Rome. After the addition of those minutes, if we console log the ZonedDateTime, it will be 2018-03-25T03:05+02:00 [Europe/Rome]. Therefore you can see Java 8 has correctly incorporated the DST time changes into its ZoneDateTime API, and developers don’t need to worry about it. In the older Java Date and Calendar APIs, we have to implement additional logic to incorporate the DST time changes.

Joda-Time Library

Joda-Time library is another alternative for Java 8 Date and Time library. This library provides similar capabilities to the Java 8 Date/Time project. The artifact can be found in Maven Central by including the below pom dependency in our project.

<dependency>     
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.4</version>
</dependency>
Create the current time from the system clock using Joda Package

To represent an exact point on the timeline, we can use the Instant from the Joda package. It represents the time as the number of milliseconds from the Java epoch of 1970-01-01T00:00:00Z. In Joda, DateTimeZone can be used to represent a time zone by specifying the zone id. Using this zone information nowUtc time will be converted to a DateTime object as mentioned below.

Create DateTime instance in Joda with time zone information

--

--

Ranmal Dewage

Software Engineer at Sysco Labs, Graduate of Sri Lanka Institute of Information Technology (SLIIT).