Why Date and Time is important to Software Engineers

Ravindran Kugan
8 min readMay 11, 2021

Time and time zones

Take a look at your watch and see the time right now. Now go and google the time for a country that is located in the opposite side of the globe to you. They might be a day ahead or behind you but most importantly both of you will not be seeing the same time for the day. The reason for this is earth’s rotation and earth rotating the sun, now lets not go deep into geography and come back to technology for a while. Who decides what the time is for each country? Well for one its their government but in our world most people do businesses around the globe, so for that there needs to be an International standard. That is why all time zones are offsets of Coordinated Universal Time (UTC). UTC is the main time standard that is used to regulate time. UTC offsets range from -12.00 to +14.00.

Time Zones (shutterstock)

Some countries have multiple time zones passing through them so they have different time for different parts of their country, ex : USA, Australia. But some countries even though have multiple time zones, they chose a single time zone for their entire country, ex : China. Now if this is a bit confusing be ready for the next part, as it explains why some countries turn their clock back or forward an hour for specific seasons.

What is daylight savings?

Daylight saving is the concept of advancing the clock by one hour midnight during the summer season to get more daytime. Countries which are located far above and below the equator to spend more time outside during summer and stay inside during the winter. So for example during summer on a specific weekend the clock will be advanced by one hour instead of being 02.00A.M the clock will be showing 3.00 A.M. As seen from the image below.

Clock is forwarded by an hour. (wikipedia)

After summer during the autumn season they will turn back the clock by one hour to its original state as there is no need for anyone to stay outside during the cold. The time will be again changed during the weekend as seen from the below image.

Clock is turned back by an hour.(Wikipedia)

What can programmers do deal with this?

As seen from the above examples software engineers not only need to work with time zones but also need to work with daylight saving as well. Who thought that something simple as telling the time would get this complicated.

So in order to make life easier for Java developers. Java has incorporated some libraries to help with the manipulation and storing of the date and time. With the addition of Java 8 a new time date library has also been added. First lets look at the old library and then look at the new ones.

Pre JDK 8

The date and time library that was used before the release of JDK 8 was not very well accepted as it had a lot of difficulties for programmers to deal with. For that reason a lot of programmers preferred to work with 3rd party libraries like JODA.

Why Old Date Time are hard to work with

The older version of these libraries are not thread safe. (Not thread safe means that in multi threading environment the codes that are written using these libraries will sometimes not give the desired outcome.).

Additional logic needs to be written when working with different time zones as there is no native way to work with them.

So as the default libraries were a bit problematic to work with, there were 3rd party API’s were used to overcome these setbacks. One widely used API for date and time manipulation is the JODA TIME.

JODA TIME

JODA Time was created to make things easier for developers working with date and time prior to JDK 8. This API helps to work with different types of calendar types which also includes Gregorian, Julian, Buddhist and more. The classes in JODA Time also helps to work with time zones and DST as well. JDK 8 time API was worked closely with the developers of JODA time.

JDK 8 TIME

JDK 8 has multiple different classes to work with dates and time. First we will take a look at ways to deal with time and date locally and then move on towards to deal with time and date that will effect with time zones and DST.

First important point to take into note with the date time classes is the fact that their constructor is private so the new key word will not be created to instantiate an object. Instead we will be creating objects using other means which will be shown with examples.

LocalDate

LocalDate can be used to store data which are birth days and pay dates. They are stored in the ISO format (yyyy-MM-dd) format with out the time.

LocalDate does not have a default constructor to create a new date object.

LocalDate localDate = LocalDate.now();
System.out.println(localDate.toString());

This will give the output 2021–05–10

As there is no constructor for LocalDate to create an instance of LocalDate we can use the following functions.

LocalDate myBday = LocalDate.of(1998,12,22);
LocalDate myBday2 = LocalDate.parse("2000-12-22");

in .parse we parse the value as a string and in of we pass the values as integers.

These are just methods to instantiate a LocalDate object. There are multiple other methods that can be used to work with getting day of the week, adding and substracting dates as well as comparisons. These can be found on the official document.

Local Time

LocalTime can be used to work with time but does need to know about the date. This does not represent a world clock or world zone time, simply it represent the time that we normally see on our wall clocks in day to day basis.

Just like with LocalDate, LocalTime also does not have public constructors to create instances.

LocalTime cTime  = LocalTime.now();
LocalTime cTime2 = LocalTime.of(4,25,23);
LocalTime cTime3 = LocalTime.parse("14:25");

As seen from the above example the first method can be used to get the current time and the second method can be used to parse in our own custom time. The third method can be used to parse the time as a string value.

Note: The LocalTime.of() has multiple (overloaded) methods that we can pass in different parameters. At minimum we must pass in hours and minutes to create an instance of LocalTime.

As with the LocalDate class LocalTime also has different mehods for adding hours mintues from getting a specific value from the time object as well. It also has methods for comparing LocalTime objects as well. Further methods can be seen in the official document.

LocalDateTime

LocalDateTime can be used to create an object that will include both the date and time. This does not include offsets from time zones as that will be handled by a separate class.

LocalDateTime t1 = LocalDateTime.now();
LocalDateTime t2 = LocalDateTime.of(cDate,cTime);
LocalDateTime t3 = LocalDateTime.of(2021,01,22,14,25);

As seen from the above example the LocalDateTime.now() method can be used to get the current date and time according to the system clock. The other LocalDateTime.of() methods can be used to create LocalDateTime instance by passing our own custom values.

Note: Just like the LocalTime class LocalDateTime class also has multiple (overloaded) methods of the LoaclDateTime.of() methods. The above examples show two ways, one is to create a LocalDateTime instance with passing a LocalDate and LocalTime object. The other is to pass in the values as integer values.

As with the above classes, LocalDateTime class also has methods that adding time, substracting time. Getting the specific day of the week and much more. The more methods can be seen from the official documents.

In the next section I will explain how to deal with time zones and DST.

How to deal with Time Zone?

We can use ZonedDateTime class to deal with time zones. In order to get or set the specific time zone we have to use the ZoneId class.

ZoneId slID = ZoneId.of("Asia/Colombo");

As seen from from the above code block. ZoneId instance can be created by parsing in the string for the specific zone. All the available zones can be received by running the below code block.

Set<String> allZoneIds = ZoneId.getAvailableZoneIds();//Get
//all zone ids
Object zoneArray[] = allZoneIds.toArray();//Convert set to object
//array.
for(int i =0;i<zoneArray.length;i++)
{
System.out.println((i+1)+" "+zoneArray[i].toString());
}

Note: Set is a Generic class and does not contain a method to view its contents. So in order to view its contents we will be the parsing the set into an Object Array. Then a simple for loop to print its contents.

Now we can have Date and Time with the respective time zones with them. Now to the converting part. To make life easier for programmers the ZonedDateTime class also contains a method to directly convert one zone time to another zone.

ZoneId lanka = ZoneId.of("Asia/Colombo");
ZoneId china = ZoneId.of("Asia/Shanghai");

LocalDateTime lTime = LocalDateTime.of(2018,12,22,12,0);

ZonedDateTime lankaZone = ZonedDateTime.of(lTime, lanka);
ZonedDateTime chinaZone = lankaZone.withZoneSameInstant(china);

With the slTime we are setting the ZonedDateTime for Sri Lankan time so to convert it to the China Time we can use China zone Id and the method withZoneSameInstant() to get the time in Shangai China at that instant as seen from the below output.

2018-12-22T12:00+05:30[Asia/Colombo]
2018-12-22T14:30+08:00[Asia/Shanghai]

As seen from the examples above the time conversions are done automatically without much hassle. The best part about ZonedDateTime is that it will automatically handle Day Light Savings as well so that the programmers do not need to worry about that.

When to use which?

Now that we have seen about date and time classes and how to use them now is the big question, when do we use the above mentioned classes. If the date or time that we are storing cares about duration then we don’t need to focus on which country or time zone that takes place. For example a code for a reminder to drink water, take breaks and taking medicines does not need to worry about time zones. When a program is to store something like date of birth and date of death it will need to cater to the time zones and needs to be stored appropriately.

REFERENCES

The following are the references that is used to create this blog.

--

--