Dealing with Date and Time in Java

Dealing with Date and Time in Java

Overview

Most of the time developers are dealing with date and time, they may be stuck into problems with time zones and daylight saving. This article will give you a brief idea about, to tackle this problem in Java. Before that, let’s clarify the terms of time zones and daylight saving.

Time Zones

In time zones, all are defined as offsets from Coordinated Universal Time (UTC) ranging from UTC-12.00 to UTC+14.00. For each time zone, an offset value is added (negative or positive) to the UTC to obtain the time of that specific geolocation. Figure 1. shows how the time zones are distributed across the world with their offset values.

Figure 1. Time Zones of the World.

Programmers tend to store the date and time on the server-side as the UTC date-time but when retrieving, need to convert back the UCT date-time according to the current geolocation.

Daylight Saving Time (DST)

This concept is used to change the time to get more work done in daylight. As an example, in the United States, on Sunday 14th of March 2021, at 2:00:00. all clocks were turned forward 1 hour and changed the times as, Sunday 2021–03–14 3.00.00. Also, on Sunday 7th of November 2021, at 2:00:00. the clock will turn backward 1 hour to change the time as Sunday 2021–11–07 01.00.00.

Dealing with Time Zones and Daylight Saving Time in Java

java.time, java.util, java.sql, and java.text packages include many classes related to date and time operations. Java 8 has introduced a new Date and Time API which includes Date and Time classes in java.time package. Among them, some classes are defined here.

LocalDate

This represents the date in year-month-day format (yyyy-MM-dd). Some of the methods declared in the LocalDate class are,

  • LocalDateTime atTime(int hour, int minute): combines this date with a time to create a LocalDateTime.
  • boolean isLeapYear: checks if the year is a leap year or not.
  • LocalDate plusDays(long daysToAdd): returns a copy of this LocalDate with the specified number of days added.
  • LocalDate minusDays(long daysToSubtract): returns a copy of this LocalDate with the specified number of days subtracted.
  • LocalDate plusMonths(long monthsToAdd): returns a copy of this LocalDate with the specified number of months added.
  • LocalDate minusMonths(long monthsToSubtract): returns a copy of this LocalDate with the specified number of months subtracted.

LocalTime

This represents the time as the format of hour-minute-second- nanoseconds (HH-mm-ss-ns). Some of the methods of LocalTime class are,

  • LocalDateTime atDate(LocalDate date): combines this time with a date to create a LocalDateTime.
  • LocalTime plusHours(long hoursToAdd): returns a copy of this LocalTime with the specified number of hours added.
  • LocalTime minusHours(long hoursToSubtract): Returns a copy of this LocalTime with the specified number of hours subtracted.

LocalDateTime

This represents both date and time as yyyy-MM-dd-HH-mm-ss-ns. Some of the methods of LocalDateTime are,

  • LocalDateTime minusDays(long days): returns a copy of this LocalDateTime with the specified number of days subtracted.
  • boolean equals(Object obj): check if this date-time is equal to another date-time.
  • static LocalDateTime of(LocalDate date, LocalTime time): obtains an instance of LocalDateTime from a date and time.

ZoneDateTime

This class is used to store all the fields related to date and time, to a precision of nanoseconds, and also a time zone with a zone offset used to handle ambiguous local date-times. Some of the methods declared in ZoneDateTime class are defined below.

  • ZoneId getZone():gets the time-zone, such as “Europe/Rome”.
  • ZonedDateTime plus(long amountToAdd, TemporalUnit unit): returns a copy of this date-time with the specified amount added.
  • ZonedDateTime minus(long amountToSubtract, TemporalUnit unit): returns a copy of this date-time with the specified amount subtracted.

ZoneId

This class specifies the time zone identifier. Some methods of ZoneId are,

  • static ZoneId of(String zoneId): obtains an instance of ZoneId from an ID ensuring that the ID is valid and available for use.
  • boolean equals(Object obj): checks if this time-zone ID is equal to another time-zone ID.

ZoneOffset

This class is used to represent the fixed zone offset from the UTC. ZoneOffset inherits the ZoneId class and implements the Comparable interface. Some of the methods declared in ZoneOffset class are defined below.

  • static ZoneOffset ofHoursMinutes(int hours, int minutes): obtains an instance of ZoneOffset using an offset in hours and minutes.
  • static ZoneOffset ofHours(int hours): obtains an instance of ZoneOffset using an offset in hours.
  • static ZoneOffset of(String offsetId): obtains an instance of ZoneOffset using the ID.

Conclusions

As per the things discussed above, classes in java.time package including ZoneId, ZoneDateTime can be used to work with time zones and daylight saving.

References

Associate Software Engineer, Undergraduate BSc (Hons) in Computer Science