Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

In legacy systems, we might need to work with dates when neither the new date and time API nor the highly recommended Joda-Time library is available.

In this short tutorial, we’re going to take a look at several approaches to see how to get the current date in pre-Java 8 systems.

2. System Time

When all we need is a single numeric value representing the current date and time, we can use the system time. To get the number of milliseconds passed since January 1, 1970 00:00:00 GMT we can use the currentTimeMillis method which returns a long:

long elapsedMilliseconds = System.currentTimeMillis();

When we want to measure elapsed time with greater precision, we can use the nanoTime method. This will return the value of nanoseconds that have passed from a fixed but arbitrary moment.

This arbitrary time is the same for all calls inside the JVM, so the value returned is useful only for computing the difference in elapsed nanoseconds between multiple calls of nanoTime:

long elapsedNanosecondsStart = System.nanoTime();
long elapsedNanoseconds = System.nanoTime() - elapsedNanosecondsStart;

3. The java.util Package

Using classes from the java.util package, we can represent a moment in time, usually by the milliseconds elapsed since January 1, 1970 00:00:00 GMT.

3.1. java.util.Date

We can represent a specific date and time by using a Date object. This contains a precision of milliseconds and the time zone information.

While there are many constructors available, the simplest way to create a Date object representing the current date in the local time zone is to use the basic constructor:

Date currentUtilDate = new Date();

Let’s now create a Date object for a specific date and time. We could use the aforementioned constructors and simply pass the milliseconds value.

Alternatively, we can use the SimpleDateFormat class to convert a String value to an actual Date object:

SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date customUtilDate = dateFormatter.parse("30-01-2020 10:11:12");

We can use a wide range of date patterns to suit our needs.

3.2. java.util.Calendar

A Calendar object can do what a Date does, and it’s better for date arithmetic computations since it can also take a Locale. We can specify the Locale as a geographic, political, or cultural region.

To get the current date, with no TimeZone or Locale specified, we can use the getInstance method:

Calendar currentUtilCalendar = Calendar.getInstance();

And for Calendar to Date conversion, we can simply use the getTime method:

Date currentDate = Calendar.getInstance().getTime();

As a fun fact, the GregorianCalendar class is the implementation of the most used calendar in the world.

4. The java.sql Package

Next, we’ll explore three extensions of the java.util.Date class that represents the equivalent SQL objects.

4.1. java.sql.Date

With a java.sql.Date object, we don’t have access to time zone information, and the precision is truncated at the day level. To represent today, we can use the constructor that takes a long representation of milliseconds:

Date currentSqlDate = new Date(System.currentTimeMillis());

As before, for a specific date, we can use the SimpleDateFormat class to convert to a java.util.Date first and then get the milliseconds using the getTime method. Then, we can pass this value to the java.sql.Date constructor.

We can simply use the valueOf method when the String representation of a Date matches the yyyy-[m]m-[d]d pattern:

Date customSqlDate = Date.valueOf("2020-01-30");

4.2. java.sql.Time

The java.sql.Time object offers access to the hour, minute, and second information — once again, with no access to a time zone. Let’s get the current Time using milliseconds representation:

Time currentSqlTime = new Time(System.currentTimeMillis());

To specify a time using the valueOf method, we can pass in a value matching the hh:mm:ss pattern:

Time customSqlTime = Time.valueOf("10:11:12");

4.3. java.sql.Timestamp

In this last section, we’ll combine both the SQL Date and Time information using the Timestamp class. This allows us to have precision down to nanoseconds.

Let’s create a Timestamp object by once again passing a long value for the current number of milliseconds to the constructor:

Timestamp currentSqlTimestamp = new Timestamp(System.currentTimeMillis());

Finally, let’s create a new custom Timestamp using the valueOf method with the required yyyy-[m]m-[d]d hh:mm:ss[.f…] pattern:

Timestamp customSqlTimestamp = Timestamp.valueOf("2020-1-30 10:11:12.123456789");

5. Conclusion

In this short tutorial, we’ve seen how to get the current date and the date for a given instant without the use of Java 8 or any external libraries.

As always, the code for the article is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.