Importance of Date and Time in Software Engineering

Hasitha Subhashana
5 min readMay 15, 2021

--

https://www.pinterest.co.uk/pin/616782111454805868/

The Russian Prime Minister came to meet President Putin and nervously told him “Sir we need to abolish these time zones”.

Putin asked: Why?

Prime Minister answered:

Ah, I can’t find myself with these times. I fly to a different country and I call home, but everyone is sleeping. Once, I woke you up at 4.00 AM in the morning, thinking it was evening. I called Angela Merkel to wish her on her birthday. But then she told me she “it was yesterday”. Also once when I wished the Chinese President a “Happy new year”, he said it's tomorrow.

Putin:
Well, these are just minor issues. Nothing to worry about.

Prime Minister:
Minor issues…! Do you remember when the Polish President was killed in a plane crash? I called to express my condolences, but the plane hadn’t even taken off yet!!! 😅 😆

Source: https://www.reddit.com/

So as you can see concepts related to times can be a bit troublesome to understand. But for Software engineers, it's a must to have a good understanding of the concepts related to date and time.

So let us begin with time zones and daylight saving.

What is daylight saving (DST)?

Every year, some countries forward their clocks by one hour from standard time during the summer and then reverse them in the autumn (back to standard time) on a government-determined date. The goal of daylight saving time is to take advantage of the long summer days. However, because dawn and sunset hours do not change significantly near the equator, countries near the equator do not follow this practice.

Even though DST can be considered as a way of conserving energy, it complicates timekeeping in travelling, record keeping, sleep patterns, and, most critically, time management in computer software.

Time zones

https://www.vectorstock.com/

A time zone is a region that maintains a standard time. For all social, commercial, and legal activities within a time zone, that time zone’s standard time is used. All-time zones are calculated from a starting point located at England’s Greenwich Observatory.

This place is also known as Greenwich Meridian. So the time at Greenwich Meridian is known as Greenwich Mean Time (GMT) / UTC.

Longitude divides the world into time zones. Each line of longitude is divided by fifteen degrees. time moves forward or backwards one hour for every fifteen degrees of longitude, depending on which you travel.

So imagine you are teleporting from Greenwich Meridian to Sri Lanka. If you teleport at 1.30 PM from Greenwich, the time in Sri Lanka will be 7.00 PM. (Since Sri Lanka is 5 hours and 30 minutes ahead of GMT/UTC).

So if you teleport from at1.30 PM to SrilTime in Sri Lanka will be since Sri Lanka is 5 hours and 30 minutes ahead of GMT/UTC. (UTC offset in Sri Lanka is 5h and 30 min.)

GMT and UTC are not the same but both of them share the same time.

If you want to know the difference in details, I suggest you visit here.

How to deal with DTS and Time zones in Java

Java 8 came with a whole new API to handle date and time. This new API consists of several useful classes to deal with not only the local time and date but also with time-zone-specific date and time as well.

Below are the classes that will help you deal with the date and time. You can find all these in Java.time package.

java.time.LocalTime class
java.time.LocalDateTime class
java.time.MonthDay class
java.time.OffsetTime class
java.time.OffsetDateTime class
java.time.Clock class
java.time.ZonedDateTime class
java.time.ZoneId class
java.time.ZoneOffset class
java.time.Year class
java.time.YearMonth class
java.time.Period class
java.time.Duration class
java.time.Instant class
java.time.DayOfWeek enum
java.time.Month enum

With Java 8 you can handle time mainly with,

  1. Local — To handle local time and date without zones (LocalDate, LocalTime and LocalDateTime).
  2. Zoned — To handle date and times with time zones (ZonedDateTime API).

Since local date and the time are what's often used, I’ll cover that first. Then we can talk about time zone specific date and time.

Handling local date and time with Java

These are some of the commonly used classes that can be used to handle the date and time where time zone information doesn't matter.

Java LocalDate class

  • Only represents a date
  • The default format of the representation is yyyy-MM-dd (ISO format).

Java LocalTime class

  • Only represents a time of the day.
  • Creates an instance from the LocalTime with the system time.

Java.time.LocalDateTime class

  • Now, this class represents the date and time. But still nothing about timezone information. This can be considered as a combination of both the above classes.
  • The default format is yyyy-MM-dd-HH-mm-ss.zzz.

Now let's see how ZonedDateTime API works.

This is the option you should use if you ever have to deal with time zones in Java. There are several time zones around the world. So to identify them Java use something called ZoneId.

Using Period and Duration

Java Period class

Java Period class is used to calculate time in years, months and days. It's widely used to get the difference between two dates or to change the values (ex: years, months and days)of a given date.

Java Duration class

Java Duration class is used to calculate time in seconds and nanoseconds.

--

--

Hasitha Subhashana
Hasitha Subhashana

No responses yet