Importance of Date/Time/TimeZone in Java

Issath Sesni
2 min readMay 19, 2021

Developers face problem to deal with time zone and daylight savings in the world. We are going to cover how to tackle this problem in this article.

What is TimeZone

All time zones are defined as offsets from UTC (Coordinated Universal Time). It is ranging from UTC-12:00 to UTC+14:00. One offset value(+/-) is added with UTC to get each location. Developers store UTC date-time in the server. So when we retrieve, have to convert UTC date-time back according to the location.

What is Daylight Savings

It means we are changing the time to get more work done in the daytime.

Eg. In US, During the summer season we forward the clock by 1 hour. After that during the autumn season we turn the clock back by 1 hour to get the original state.

How to deal this in java

Earlier developers used several date-time packages such as java.time(provided only few operations), java.util, java.sql. They are not thread safe and provided few operations. To overcome this problem java 8 introduced new date-time API under java.time package. Most important classes among them are:

  1. Local − Simplified date-time API with no complexity of timezone handling. When time zones are NOT required, use
  • LocalDate:- This represents date in year-month-day format (yyyy-MM-dd)
  • LocalTime :- This represents the time as the format of hour-minute-second- nanoseconds (HH-mm-ss-ns).
  • LocalDateTime API :- This represents both date and time. (yyyy-MM-dd-HH-mm-ss-ns).

2. Zoned − Specialized date-time API to deal with various timezones. When time zones are to be considered use,

  • ZoneDateTime :- It is used to store all the fields related to date and time.
  • ZoneId :- It specifies the time zone identifier.
  • ZoneOffset :- It is used to represent the fixed zone offset from the UTC.

More details refer Java 8 Documentation.

--

--