Importance of Date and Time in Programming

Ramsunthar Sivasankar
Nerd For Tech
Published in
6 min readMay 16, 2021

--

Photo by Tristan Colangelo on Unsplash

Everything in the world is entirely depend on Date and Time. Today it is 16th of May 2021 and now it is 12.14 AM but is it the same time in the entire world, No right, why because obviously we know that the earth is a globe shape and Earth rotates around its axis as it revolves around the sun. So sun can’t show up to all the spot in the earth at the same time. So half of the earth will have day time and half of the earth will have night time. There are 24 time zones in the world. One day is broken down into seconds and computed to get the precise time of a certain location. But it isn’t that simple. The 24 time zones, one for each hour of the day, are theoretically drawn vertically over the globe like longitudes. Everything should follow a standard to avoid conflicts. So that is where UTC take place. UTC is the primary time standard by which the world regulates clocks and time and its range is from the westernmost (−12:00) to the easternmost (+14:00).

(image: https://en.wikipedia.org/wiki/List_of_UTC_time_offsets)

You there are some countries with multiple timezone and there are 23 countries with at least two time zones. The country with the most time zones is France, mostly due to its various territories around the world. But some even though these countries have multiple timezone, mostly they use only one timezone. You know there are some countries will change their times. Lets discuss about that.

Day Light Saving

The practice of advancing clocks (usually by one hour) during warmer months, known as daylight savings time or summer time in the United States and Canada, and summer time in the United Kingdom, European Union, and other countries, is known as daylight savings time.To revert to standard time, clocks are often adjusted forward by one hour in the spring (“spring forward”) and backward by one hour in the autumn (“fall back”). As a result, in late winter or early spring, there is one 23-hour day and one 25-hour day, while in the fall, there is one 25-hour day.

As a programmer he or she must aware of this when dealing with date and time because it might lead to a devastating situation in the project. So lets talk how programmers deal with this.

Java 8 Date Time API

A whole new date and time API was introduced in Java 8. The new Java date and time API may be found in the java.time package, which is included in the standard Java 8 class library. The key difference in the Java 8 date time API is that date and time are now expressed by the number of seconds and nanoseconds since January 1, 1970, rather than a single amount of milliseconds. A long represents the number of seconds, which might be positive or negative. An int represents the number of nanoseconds, which is always positive. Many of the classes in the new Java date time API will use this new date and time format.

There are some useful commonly used classes in this API such as LocalDate, LocalTime, LocalDateTime and ZonedDateTime. Lets discuss one by one.

LocalDate

The LocalDate class in the Java 8 date time API defines a local date, which is a date that does not have a time zone associated with it. A local date is a date that refers to a specific day of the year, such as a birthday or an official holiday. This class is in the java.time package. So its fully qualified class name is java.time.LocalDate and this class in immutable. So it will return a new LocalDate for all the calculations made on a LocalDate class. The default format is (YYYY-MM-DD).

There are several ways to create such as,

  • Getting the current date using now keyword
LocalDate localDate = LocalDate.now();

So if we print the variable localDate , we will be able to get the date when the the printing is happening.

  • Creating by giving the information manually such as date, month and year
LocalDate localDate1 = LocalDate.of(2021, 05, 16);

we also can use the following methods to get the date information,

  • getYear()
  • getMonth()
  • getDayOfMonth()
  • getDayOfWeek()
  • getDayOfYear()

LocalTime

The Java 8 date time API’s LocalTime class specifies a specified time of day without any time zone information. For example, 10:00 a.m. The LocalTime instance can be used to express when school or work starts in different nations if the time in the relevant nations is more important than the UTC. You could express that as UTC with a time zone, but you could alternatively use a LocalTime object without the time zone information.

Here also there are several methods to create this,

  • Getting the current time using now keyword just like LocalDate
LocalTime localTime = LocalTime.now();
  • Creating LocalTime by giving the information manually such as amount of hours, minutes, seconds and nanoseconds
LocalTime localTime1 = LocalTime.of(02, 11, 59, 1200);

There are some methods which used to get time information as follows,

  • getHour()
  • getMinute()
  • getSecond()
  • getNano()

LocalDateTime

The LocalDateTime class (java.time.LocalDateTime) in the Java 8 date time API represents a local date and time with no time zone information.The LocalDateTime class is a mix of the Java 8 date time API’s LocalDate and LocalTime classes.

Here also there are several methods to create this,

  • One of the static factory methods of the LocalDateTime object is used to construct it.
LocalDateTime localDateTime = LocalDateTime.now();
  • Another way to create a LocalDateTime object is to create it based on a specific year, month, day etc.
LocalDateTime localDateTime2 = LocalDateTime.of(2021, 05, 16, 02, 16, 36, 1100);

There are some methods which used to access date and time fields information as follows,

  • getYear()
  • getMonth()
  • getDayOfMonth()
  • getDayOfWeek()
  • getDayOfYear()
  • getHour()
  • getMinute()
  • getSecond()
  • getNano()

ZonedDateTime

The Java 8 date time API’s ZonedDateTime class represents a date and time that includes time zone information.

Here also there are several methods to create this,

  • First one is to call the now() method of the ZonedDateTime class.
ZonedDateTime zonedDateTime = ZonedDateTime.now();
  • The of() method, which can create a ZonedDateTime object from a specific date and time, is another option for creating a ZonedDateTime object. This might signal the commencement of a worldwide event, such as a conference or a rocket launch. To cope with time zones, we may utilize the ZonedDateTime class. We must utilize the ZoneId class to acquire or set the specific time zone.
ZoneId zone= ZoneId.of("Asia/Colombo");

It is also possible to give the UTC vaule as the parameter to get the zone. Now lets create zonedDateTime object,

ZonedDateTime zonedDateTime1 = ZonedDateTime.of(2021, 05, 16, 02, 28, 59, 1345, zone);

Since we can get the date and time for a particular zone, lets try to convert the time for the country which has daylight saving prob.

So the output is,

2021-05-16T02:46:09.538271700+05:30[Asia/Colombo]
2021-05-15T22:16:09.538271700+01:00[Europe/London]

So in Java, ZonedDateTime handles Day Light Saving by itself. Usually its better to use where the situation is critical like International meeting, Funeral Time, Birth Time (happened overseas) or something like that. For the normal purpose, we can use LocalDateTime

--

--

Ramsunthar Sivasankar
Nerd For Tech

MSc student of Greenwich University || Software Engineer