Dealing with Date, Time & Time Zones in JAVA

Arshad Suraj
5 min readMay 19, 2021
Photo by Aron Visuals on Unsplash

What is Time Zones?

A time zone is a region with a standard time throughout that is used for all social, commercial and legal purposes within that region. As per 24 hours, Earth is divided into 24 time zones by longitude. Each line of longitude is separated by fifteen degrees. Therefore time difference between 2 adjacent time zone is one hour. As a result depending on which way one travels, time moves forward or backwards one hour for every fifteen degrees of longitude.

What is UTC?

UTC is the abbreviation for Coordinated Universal Time. It is a global standard for determining all time zones. UTC is based at the Prime Meridian(0 longitude). It is unaffected by any sort of daylight savings time. According to the UTC standard, each time zone has its own UTC offset. The range of UTC offsets is -12.00 to +14.00. Every time zone’s time will differ depending on the offset.

for example, Sri Lanka lies in the UTC +05.30 time zone, which means that Sri Lankan time is 5 hours and 30 minutes behind than UTC time. As a result, if the current UTC time is 10.30 AM, Sri Lankan time is 4.00 PM.

What is Daylight Saving Time?

Daylight saving time (DST) is the practice of settings the clocks forward (usually by one hour) during summer months so that darkness falls at a later clock time. Then in autumn, clocks will be set back backward to return to the standard time. As a result, there is one 23-hour day in summer and one 25-hour day in the autumn.

Today, approximately 70 countries utilize Daylight Saving Time in at least a portion of the country.

As a result of understanding these concepts, we can feel that if we don’t handle the time and time zone properly in software engineering, we may encounter major problems.

For Example: Consider an application that stores the time of a person’s death as its local time. So, if John Doe dies at 4.00 PM in Sri Lanka, our application logs that John Doe died at 4.00 PM, correct?
Then if a user of this application from the United States checks the death time of John Doe after hearing the news, it will show 4.00PM. But, the current time in the United States is 6.00 AM. As a result, the user will think that John Doe will die at 4 p.m. Isn’t it strange?

Therefore, How to deal with Time and Date?

Before JDK8

Prior to the release of JDK 8, even though prior JDKs had “date and time library”, most programmers moved to 3rd-party libraries. Because programmers had to face numerous challenges and make lot of manual configurations to work with date and time.

Old JDKs had following issues,

  • Date and time classes are not thread-safe.
  • No methods to deal with time-zones.
  • There are only few date operations were there.
  • The API design of some date and time classes was bad such as years starts at 1900.

JAVA 8 Time & Date API

To address the challenges that existed in previous versions, Java 8 introduced a new Date and Time API with many capabilities and classes for working with dates and time.

LocalDate

LocalDate classes can be used to store dates in ISO format (yyyy-MM-dd).

for example: to store birthday, day of Christmas

LocalDate does not capable of store time.

Since LocalDate doesn’t have a default constructer we cannot create instance directly. Therefore, to create a LocalDate instance that represents current day use the following code.

LocalDate today = LocalDate.now(); // gives the current date on
//local machine

To store a specific day,

LocalDate Christmas = LocalDate.of(2021,12,25);
or
LocalDate Christmas = LocalDate.parse("2020-12-25");//parsing as a
//String

There are some methods available to do several operations with dates. some of them are,

LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plusDays(1); // Add given days
LocalDate yesterday = today.minusDays(1); // subtract given days

please refer to below link to know more about LocalDate,

LocalTime

LocalTime classes can be used to store time without date and time zones. It’s store time similar to our analog wall clock.

for ex: to store sunrise of the day

Same as the localDate, localTime also doesn’t have a default constructer. So to create instance,

LocalTime now  = LocalTime.now();
LocalTime sunrise = LocalTime.of(5,25,23); //hh:mm:ss
LocalTime sunset = LocalTime.parse("18:05:30"); //parsing as a
//string

Like LocalDate, there are some methods in LocalTime that can be used to do various operations on Time. Please Refer to,

LocalDateTime

LocalDateTime is a mixture of the LocalDate and LocalTime classes.

for ex: Rocket departure time (Date & Time)

Time zone operations are not possible with this class. To create an instance,

LocalDateTime now = LocalDateTime.now();or
LocalDate today = LocalDate.now();
LocalTime current = LocalTime.now();
LocalDateTime now = LocalDateTime.of(today, current);
or
LocalDateTime now = LocalDateTime.parse("2007-12-03T10:15:30");

Please refer to the below link to know more information on variables and other methods in this class.

ZonedDateTime

With date and time additionally we can also store zone information in ZonedDateTime class.

for ex: Lunar eclipse time in Sri Lanka (Date, time, Zone)

To create ZonedDateTime instance,

ZonedDateTime now = ZonedDateTime.now();or
LocalDateTime t1 = LocalDateTime.parse("2007-12-03T10:15:30");
ZoneId srilanka = ZoneId.of("Asia/Colombo");
ZonedDateTime t2 = ZonedDateTime.of(t1,srilanka);
or
ZonedDateTime now = ZonedDateTime.now("2007-12-03T10:15:30+01:00[Europe/Paris]");

Note: There are lot of ways to create an instance of this class.

Time conversion

Above we learned how to create an object of ZonedDateTime class. Now let’s look at how to get convert a time in different time zones.

ZoneId moscow= ZoneId.of("Europe/Moscow");
ZoneId newyork = ZoneId.of("America/New_York");
ZonedDateTime SL_Time = ZonedDateTime.now();// local machine is in
//Sri Lanka

ZonedDateTime New_York = SL_Time.withZoneSameInstant(newyork); //Represent new yok time
ZonedDateTime Russia = SL_Time.withZoneSameInstant(moscow); //Represent Russia time (Moscow)

NOTE

  • Whenever you’re asked to save a time in a server, save it in UTC. As a result, users in different time zones can convert this time to their own time zones and perform the operations they require.

Keep Learning ❤️

--

--