Introduction to Java Date & Time

Oshan Vikasith
5 min readMar 1, 2022
Photo by Aron Visuals on Unsplash

In this article we are going to discuss mainly about handling date & time through java.util.date package, java.sql.date package and the newly constructed java 8 data and time API.

Before discussing each of these topics it’s better to know about the epoch time.

It is essential to know the theoretical aspects behind the concept epoch, because it is widely used in java when calculating date and time.

Epoch Time

The epoch time (unix time, posix time, unix epoch time, seconds since epoch) is the number of seconds elapsed since January 1, 1970 at midnight UTC, excluding leap seconds. In epoch time each day is treated as if it contains exactly 86,400 seconds. So this is not a true representation of UTC time.

Epoch time is widely used in operation systems, file formats and in Unix-like operating systems.

Epoch time 0 is midnight January 1st 1970 (in ISO 8601: 1970–01–01T00:00:00Z).

Some systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).

The reason we discussed epoch time is that in java when calculating date and time, epoch time is used so this introduction will help to understand the concepts more clearly.

Now we are going to discuss the classes used in java in order to calculate date and time.

Java.Sql.Date

The java.sql.Date class inherits java.util.Date class. In java.sql.Date , it will only deliver the date as an output.

Java.sql.date will represent only the SQL date which as years, months & date. Time is not kept within the sql.Date class.

In java.sql.Date the date will be stored in milliseconds from the 1st of January 1970 (epoch time).

It is better to use sql.date class when handling data related to SQL specific requirements. It adds formatting and parsing operations to support JDBC.

This class does not hold any time zone information.

Constructor of the java.sql.Date class

Date(long milliseconds) — construct a date using milliseconds time value.

Output :- 2022–03–01

Methods (java.sql.date)

void setTime(long time)

Changes current SQL date to given time

Instant toInstant()

Convert current SQL date into instant object

LocalDate toLocalDate()

Convert current SQL date to locale date object

String toString()

Convert this SQL date object to a string

static Date valueOf(LocalDate date)

Returns SQL date object for the given Local Date.

static Date valueOf(String date)

Returns SQL date object for the given String

Java.util.Date

The java.util.Date class represents data and time in java.

Java.util.Date represents a particular moment in time with millisecond precision since 1st of January 1970 epoch time. (00:00:00 GMT).

The java.util.Date class implements Serializable, Cloneable and Comparable<Date> interface.

It is inherited by java.sql.Date, java.sql.Time and java.sql.Timestamp interfaces.

Constructor of the Java.util.Date Class

Date() Initializes the object with the current date and time

Date(long millisecond) Initializes the object with the specified number of milliseconds since January 1, 1970, 00:00:00 GMT.

Output : Tue Mar 01 12:44:35 IST 2022

Methods (java.util.date)

boolean after(Date date)

Check if the current date is after the given date.

boolean before(Date date)

Check if the current date is before the given date.

Object clone()

Return the clone object of the current date.

int compareTo(Date date)

Compare current date with the given date.

boolean equals(Object date)

Check whether the current date and the given date are equal.

long getTime()

returns the time represented by this date object.

int hashCode()

returns the hash code value for this date object.

void setTime(long time)

Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

String toString()

Converts this Date object to a String.

Java Date

Due to the fact that some core issues were identified in java.util.Date package and java.util.Calendar package, in java 8 a new API was introduced to handle date and time.

These are the issues which arrived because of the previous data and time APIs

  1. Issues with regards to thread safety.
  2. API design and ease of understanding.
  3. Issues with regards to dates and times in different time zones .

Thread Safety

Date and calendar classes are not thread safe. Developers have to dedicate additional time and logic to make it thread safe. Newly constructed APIs are immutable and thread safe.

API design and ease of understanding

Design of the data and calendar APIs are poorly constructed. Consist of a wide variety of utility methods to support date and time related operations.

Issues with regards to dates and times in different time zones

In the previous API, had to implement additional logic when it comes to execute time zones specific requirements. Newly constructed API is included with Local and zonedDate/ time APIs.

Now we are going to discuss about the newly introduced java date and time API.

Following are some of the classes introduced newly to java 8.

  1. Java LocalDate class.
  2. Java LocalTime class.
  3. Java LocalDateTime class.
  4. Java MonthDay class.
  5. Java OffsetTime class.
  6. Java OffsetDateTime class.
  7. Java Clock class.
  8. Java ZonedDateTime class.
  9. Java ZoneId class.
  10. Java ZoneOffset class.
  11. Java Year class.
  12. Java YearMonth class.
  13. Java Period class.
  14. Java Duration class.
  15. Java Instant class.
  16. Java DayOfWeek class.
  17. Java Month enum.

Local: Simplified date-time API with no complexity of time zone handling.

Zoned: Specialized date-time API to deal with various time zones.

Each of these classes consist of methods which will help to get relevant tasks done quite efficiently. You can refer to those by going through the referenced links attached to this article.

To work with local date and time, api provides classes such as below in order to get the required task done efficiently.

  • LocalDate
  • LocalTime
  • LocalDateTime

Following class is implemented in order to manage date and time with respect to time zones. There are 40 different time zones specified.

  • ZonedDateTime

Hope you had a better understanding regarding java date classes, Hope to see you again in the future with an another interesting article.

Here I have included the sources which I referred.

java.sql.Date — Javatpoint

Java Date and Time (w3schools.com)

java.util.Date — Javatpoint

Java 8 date time api tutorial — W3schools

--

--