Java Date Time API

Anuradha Gunasinghe
3 min readMay 16, 2021

--

Java 1.0 was released a simple java.util.Date class was provided to deal with time. By definition, this class actually doesn’t represent a date but a specific instant in time, with millisecond precision

In java version 1.0 year start from 1900. months are zero index based. But developers are faced many errors when using date.

For example to get an instance representing the date “16 May 2021”, you would have to call this constructor :

Date myDate = new Date(121, 4, 16);

Here years were so far from the reality, months were zero-index based meanwhile days were not.

Another problem is given as input to the constructor. In fact, you can call the constructor passing the date “31 May 2021” and get an instance with a date that actually is “01 Jun 2021”.(https://docs.oracle.com/javase/10/docs/api/java/util/Date.html)

After Java version 1.0 they introduced a Java 1.1 version with the java.util.calander class. In new version the year offset is managed by the Calendar class and many constants were added to deal with days or months.

However developer faced a again lot of problem

  • months are still zero index based
  • calendar class is not thread safety
  • java.text.DateFormat were introduced to parse the string dates but also its not thread safe
  • it is still problematic to do some calculations as intervals or differences between dates in a simple manner

To deal with those issues and fix them from Java 8. now a days they are provided a new package, java.time, in which you can find all the Date Time API classes, all are immutable implementations for thread-safety, supporting standard time concepts as date, time, instant and time-zone and provide an effective API for the developers.

Let’s see the core classes to deal with dates and time starting from Java 8

LocalDate : its instance is an immutable object representing a plain date without time of the day and store the date in the YYYY-MM-DD format.

LocalTime : it is similar to LocalDate, but it represents only the time of the day without time zone details and stores the time in the HH:mm:ss.nanos format.

LocalDateTime : this is the combination of the previous two, holding both date and time parts without timezone details. The datetime is stored in the YYYY-MM-DDThh:mm:ss.

Instant : it is a specific point in the continuous timeline. It represents the seconds passed since the Epoch time 1970–01–01T00:00:00Z and internally stores two values :

  • a long value representing seconds from the Epoch time
  • an int value representing the nanoseconds of seconds

java.time.ZonedDateTimeLocalDateTime combines with the time zone provided using ZoneId class.

java.time.OffsetTime — Handles time with a corresponding time zone offset from Greenwich/UTC, without a time zone ID.

java.time.OffsetDateTime — Handles a date and time with a corresponding time zone offset from Greenwich/UTC, without a time zone ID.

java.time.Clock — Provides access to the current instant, date, and time in any given time zone.

java.time.Duration — Difference between two instants and measured in seconds or nanoseconds and does not use date-based constructs such as years, months, and days, though the class provides methods that convert to days, hours, and minutes.

java.time.Period — To define the difference between dates in date-based values (years, months, days).

java.time.ZoneId — specifies a time zone identifier and provides rules for converting between an Instant and a LocalDateTime.

java.time.ZoneOffset — Specifies a time zone offset from Greenwich/UTC.

--

--

Anuradha Gunasinghe

Software Engineer @ WTS, Bachelor of Engineering (BEng) Honours in Software Engineering Graduated from University of Westminster