Java 8 Date/Time API

Erdem Akkuzu
5 min readApr 18, 2021

The date/time API is a very detailed and important topic in Java. Especially for applications with users in different locations, it is of great importance to use this API effectively and to perform the conversion operations correctly.
In this article, we will examine the classes and features that have come with Java 8 and take a look at a few examples.

The classes in the new packages that came with Java 8 are immutable and thread-safe.

Let’s take a look at these classes in the packages we’ve mentioned.

1) java.time.LocalDate

The LocalDate class keeps the date information of the location. This class should not be confused with the Date class in java.util.Date package.
Objects created from the Date class have both date and time information, whereas objects created from the LocalDate class have only date information.
It has a private constructor so to create an instance we use LocalDate.now() or LocalDate.of(…) with different parameters

As you can see, LocalDate only contains the date information, while Date also holds some other additional information.

With LocalDate.now() we can create a LocalDate instance with the system’s current date information. Also, we can create a LocalDate instance with specific date information by giving parameters to LocalDate.of(…) static method.

It is possible to create an instance from a String value with LocalDate.parse() method.

Also, we can add days, months, or years by using plus or minus methods. But remember that LocalDate class is immutable so you need to reassign the new value to your variable.

2) java.time.LocalTime

This class holds only time information (hours-minutes-seconds). The way of creating an instance is similar to the LocalDate class. Also, it has similar methods such as parse and plus… methods.

3) java.time.LocalDateTime

LocalDateTime stores both LocalDate and LocalTime information.It has similar methods like now() and parse(). But also, the LocalDateTime.of() method can take LocalDate and LocalTime instances as a parameter so you can create a new instance by using them too.

4) java.time.ZoneId

There may be date and time differences between locations. For this reason, it may be necessary to specify the zone information of the location while calculating the times in different locations.

The ZoneId helps us to get the zone information of the desired location. It is possible to get the system’s zone id by using the ZoneId.systemDefault() method.

Also, we can see all possible zone ids inside ZoneId class with ZoneId.getAvailableZoneIds() method. It returns a set of String including all available zone ids.

LocalDateTime.now() method also accepts ZoneId. So if you give one ZoneId as a parameter, you can get the current date and time in a specific zone.

5) java.time.ZonedDateTime

ZonedDateTime class includes the date, time, and Zone id.

So we can think like ;

LocalDateTime includes Local Date + LocalTime

ZonedDateTime includes LocalDateTime + ZoneId

We can get the current ZonedDateTime of our location by using the ZonedDateTime.now() method. Or we can get it by giving LocalDateTime and ZoneId instances as a parameter to the ZonedDateTime.of().

6) java.time.Instant

In some applications, you may need to return the date and time in UTC 0 format to ensure the date and time consistency.

For example, you have an application and this application has many users around the world. In order for all users to see the time of the events in the application correctly according to the time of their location, you may need to store the time of the events in UTC 0 format and convert it according to user’s zone id while showing it to the user.

java.time.Instant class is used to convert date and time to UTC 0 format. And you can convert your ZonedDateTime to Instant by using the ZonedDateTime.toInstant() method. Or you can use Instant.now() method to get the current date and time according to UTC 0.

7) java.time.Period

The period class represents the specific period for LocalDate (Year, Month, Day). It can be used to calculate the difference between dates. And also you can use the LocalDate.plus() or LocalDate.minus() method by giving a specific Period (Year, Month, Day) as a parameter.

**Note that; it can not be used for LocalTime. To do the same operations for LocalTime there is another class. We will see it soon :).

8) java.time.Duration

The Duration class, like the Period class, is used to specify a specific time interval (hours, minutes, seconds). We use this class for the LocalTime.

9) java.time.temporal.ChronoUnit

java.time.temporal.ChronoUnit is the enum of the standard set of date period units. We can use it to manipulate LocalDate, LocalTime, or LocalDateTime.

In the example below, you can see the minute difference between my location and the location of the city of Rome calculated using ChronoUnit.

10) java.time.format.DateTimeFormatter

In some cases, you may not want to use the default format for a specific time or date. For this, the DateTimeFormatter class will do the trick. It is necessary to describe the desired format to the .ofPattern () method of the DateTimeFormatter class with parameters.

Here you can see a few common patterns related to date and time;

A few examples of using DateTimeFormatter;

--

--