Importance of Date and Time in the programming world.

Roshni Silva
6 min readDec 11, 2021

--

Date and Time, are just two different words. sometimes you might feel like this but most of the things in this world are totally dependent on a date and time. For me, now it's the 10th of December 2021 and here the time is 4.38 pm. So does that mean each and everyone on this globe has the same time?

No………Not even the time, but some humans in the world almost passed to 10th December why because as far as we know this Earth is globe shape and its rotates around its axis and revolves around the sun. Due to this rotation fact, we can understand that the sun can’t face everywhere on the earth at the same time. So some of the people in the world enjoy their day time while some enjoy their the night.

Now for a moment let’s imagine if the entire world has a one time zone. So what will happen now? for some places it would be noon, for some it would be early morning and for some, it would be evening. Since the different places of the earth enter and exit daylight at different times, so different time zones were introduced. These time zones are measured from a starting point centered at England’s Greenwich Observatory and this point is called Greenwich Meridian or Greenwich Mean Time (GMT) and according to this time zone, each is given an offset which is either positive or negative value.

Time Zones : https://en.wikipedia.org/wiki/File:Standard_Time_Zones_of_the_World_(October_2015).svg

So what is this UTC? UTC is standard for Universal Time Coordinated and this is not a time zone but the primary time standard used to represent time zone. The time of each and every country is calculated based on this.

Let’s consider Sri Lanks as the example: we use IST (Indian Standard Time) and our time is 5 hours and 30 minutes ahead of UTC so we can represent our time as UTC + 5.30.

Before jumping into the programming world, we need to know one more fact which is DST.

DST is standard for Daylight Saving Time, It is the practice of advancing clocks (by 1 hour)during the summer month, therefore, darkness comes at a later clock time and backward by one hour during the autumn.

As a programmer, having an understanding of how data and time work in different places in the world is really important when we handle date and time-related data.

You are in Sri Lanka. Now just assume a scenario where you have to implement a system to keep track of the arrival and departure times of employees to a global company and you implemented that system and deployed it into the cloud but you keep all the date and time records according to your time zone. This will create a problem if the client is from a different region such as the US wants to see records according to his time zone, but your application represents the records in Sri Lankan time. Now you are in a big mess 🤯…

Now you can feel how important is to handle dates and times in a correct manner in the programming world. If you don’t handle date and time in a correct manner you will be ended up with large issues.

In a programming world, we have lots of programming languages but via this story, I take you guys through only JAVA and Java.time, java.sql, java.text and java.util packages contain lots of classes to represent date and time. Let’s see what are some classes available in java to handle date and time.

ARE YOU READY TO FOLLOW?

let’s go one by one

Java Classical Date/Time API

java.time.LocalDate class represents the date and time in java and this provides constructors and methods to deal with date and time. we can use this to represent time according to the relevant time zone.

java.sql.Date class This is only capable of representing the date in YYYY-MM-DD format in java and it inherits java.util.Date class. Since it represents the date that can be sorted in the database, JDBC is used this mostly.

java.util.Calendar classThis class provides methods to converting date between a specific instant in time and set the calendar fields and this class is also an abstract class that inherits object class and implements the comparable interface. There are lots of pre-defined methods in this class and getInstance() method is used to get the instance of the calendar according to the current time zone.

java.util.TimeZone classObject class is inherited by this class. This TimeZone class is used to represent a time zone offset and also this helps to figure out daylight saving. This class also contains some of the pre-defined methods and method getDisplayName() is used to get the default time zone.

Java 8 Date/Time API

java.time LocalDate classIf a programmer uses this class, this provides a date with a default format of YYYY-MM-DD. This class also inherits an object class and an immutable class. We can use LocalTime object without the timezone information. Out of many methods available in this class now(), the method is used to get the current date.

java.time LocalTime class — This class has the ability to specify a specific time of the day without any timezone information. this class includes several pre-defined methods out of these now() is used to get the current time. This LocalTime class is an immutable class and it represents the time with the default format of hour-minute-second.

java.time LocalTime class — This class represents a local date and time both without any information about the timezone. This class is somewhat a combination of java 8 LocalTime and LocalDate classes and it represented date-time, with the default format as YYYY-MM-DD dd-HH-mm-ss.zzz. This is an immutable and inherits object class. now() method in this class is used to get the current date and time.

java.time ZoneDateTime class — This represents date-time with a timezone. This class is used to store date and time fields to a precision of nanoseconds and timezone with zone offset. This is mostly used to handle ambiguous local date-time.

It’s not really simple as you can think when you working with date and time. As a programmer, when you deal with date and time, REMEMBER to choose the right date and time class based on your requirement and make sure you are aware of the data format that you want to keep for the date and time. Lastly, I want you guys to always be aware of time zones and when you should use these time zones while handling dates and times.

See you Later!!!

--

--