Handling Date and Time in Java

Photo by Age Barros on Unsplash

Time is a more precious power in the world. We all are carried our life to follow by the time.

When dealing time with software it’s tricky hard to keep maintaining the current date and time. Because the time zone is different everywhere. Developers were hard to build the software for different time zone.

In the early stages, In java, the date and time data are stored in the form of a long data type number. Because of that, the number of seconds can be shown as positive and negative ( long represented both positive and negative signs).

There are some pre-build libraries (classes )are used to define the time zone in applications.

Those are:

  1. java.util.Date
  2. java.util.TimeZone
  3. java.util.Calendar
  4. java.text.SimpleDateFormat

These libraries are available in java.util, java.time, java.sql, java.text packages.

These libraries are used up to java 7. But when developers have used these legacy classes to build an application they faced some difficulties.

Those are :
1. Date formatting Issue
The Date class used to represent a date but it represents time( hours, minutes, and seconds )also.

Example of Date shown
The output of the above example

2. Time zone handling issue
The date doesn’t associate with the time zone. It takes the default time zone. Hence we cannot represent a date for different time zones.

3. No Thread-safe
When we trying to use Date and time formatting in java, java.util classes are not thread-safe. Therefore it is difficult to handle for use.

4. Zero index-based approach Fails.
Date classes used to zero index for a month, this will take several bugs in sometimes.

Example code for showing zero index-based approach fails
The output of the example code

Hence the reason, Java 8 provides Date and Time API to solve these drawbacks with legacy classes.

Java 8 Date Time API

How does java 8 handle Date?

Java 8 introduces 2 classes for handle those issues ;

  • ZoneId implements the IDs of time zones. For example ZoneId.of(“Asia/India”).
  • ZoneDateTime implements the human-readable representation of an instant in the ISO 8601 calendar, in a given time zone. For example: Instant.now().atZone(zoneId)

Java 8 provides Date Time API to developers for build applications. This Date Time API located in java.time package.This java.time package contains some sub-packages those are having multi utilities. For that java.time.format package contains some classes used to parse and format dates form to string.

The main changes in Java 8 is no long data type is used to represent by single number of milliseconds.

But the number of nanoseconds is always positive and its representation by using int data type. It used before and after java 8

In java.time package contains various class likes Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth, and so on.

  1. java.time.LocalTime- It’s an immutable class and represents the time with the default format of hour-minute-second. only represent the time based on ISO 8601 calendar.
  2. java.time.LocalDate- Year-month-date represents in the ISO calendar. Only the date will be shown (Without shown in time). This time was read in a human-readable manner.
  3. java.time.LocalDateTime- Date-time object that represents both date and time with the format yyyy-MM-dd-HH-mm-ss.zzz.
  4. java.time.ZonedDateTime- It’s represented with date and time-zone. It’s used to store all date-time fields to precision nanoseconds. (LocalDateTime combine with timezone to represent it)
  5. java.time.OffsetTime- Represents the time view format as hour-minute-second as time zone offset without showing time zone ID.
  6. java.time.OffsetDateTime- It represents the Date with the corresponding time zone without showing zone ID.
  7. java.time.Clock- Provides to access the current instant, date, and time using any given time zone.
  8. java.time.Instant- Represent the specific moment on the timeline.
  9. java.time.Duration- Used to measure the time in seconds and nanoseconds. Doesn’t use the date-based construct such as years, months, and day rather provides methods to convert in days, hours, and minutes.
  10. java.time.Period- Used to define the difference between dates in date-based value (years, months, days)
  11. java.time.ZoneId- specifies a time zone identifier and provides rules for converting between an Instant and a LocalDateTime.
  12. java.time.ZoneOffset- specifies a time zone offset from Greenwich/UTC time.

Date Time APIs Operations

LocalDateTime, LocalDate, LocalTime these objects are immutable objects, hence that we cannot apply those operators, But it is important to add, subtract, and calculations for date and time, Java has provided operations for the objects. For that, plusX() can use for addition, minusX() can use for subtraction. X that can be replaced “Years, Months, Hours, Days”. And “isBefore()” or “isAfter()” methods are used for comparing with dates.

Formatting & Parsing Date and Time

Using java.time package to get the date and time for the different time zone based on each geographical location. But this not enough to represent the time and date, so need a standard way to represent the time zone.

For that geographical location difference, people using a variety of standard languages. So that reason application should be adjustable to represent the local language and format. Java provides java.util.Locale class that supported in the Java environment to represent the languages and countries. So first it setting the Locale to format time into the local format, then java gave java.time.format.DateTimeFormatter, to present the date and time as a standard way or custom way.

Let’s take an example code to see formatting the Date according to different countries and languages.

Example Code
Output

The output will be shown in different date formats in different countries and languages.

Let’s see another example :

Above code, set the locale to some countries with different date formats and languages, Then use DateTimeFormatter to set the different date format based on the locate. Finally, format the given date and assign it to a String object for representation.

The output will like this:

Output

Software Engineer at Virtusa (Pvt) Ltd.