Course – LS – All

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

>> CHECK OUT THE COURSE

1. Overview

The Unix time is the total number of seconds that have passed since 00:00:00 UTC on January 1, 1970. This point in time is named the Unix epoch. The Unix time helps to represent date and time in programming.

In this tutorial, we’ll learn how to use the legacy Date API, Date Time API, and Joda-Time library to retrieve Unix time values in Java.

2. Using the Legacy Date API

The Date API provides a class named Date, which provides a method to get the current time. We can get the current Unix time by dividing the current time in milliseconds by 1000L.

Let’s see an example that uses the Date class to retrieve current Unix time:

@Test
void givenTimeUsingDateApi_whenConvertedToUnixTime_thenMatch() {
    Date date = new Date(2023 - 1900, 1, 15, 0, 0, 0);
    long expected = 1676419200;
    long actual = date.getTime() / 1000L;
    assertEquals(expected, actual);
}

Here, we create a new Date object and initialize it with a fixed date and time. Next, we invoke the getTime() on the Date object to get the time in milliseconds. Then, we divide the time in milliseconds by 1000L to get the Unix time.

Notably, the standard Unix time timestamp is in seconds since epoch and not milliseconds.

Finally, we assert that the result is equal to the expected Unix time.

3. Using the Date Time API

The new Date Time API from Java 8 provides the LocalDate and Instant classes to manipulate date and time. We can get the current Unix time by invoking getEpochSecond() on the Instant object:

@Test
void givenTimeUsingLocalDate_whenConvertedToUnixTime_thenMatch() {
    LocalDate date = LocalDate.of(2023, Month.FEBRUARY, 15);
    Instant instant = date.atStartOfDay().atZone(ZoneId.of("UTC")).toInstant();
    long expected = 1676419200;
    long actual = instant.getEpochSecond();
    assertEquals(expected, actual);
}

Here, we create a LocalDate object to represent a fixed time. Next, we pass the LocalDate object to the Instant object to indicate the start of a day.

Furthermore, we invoke the getEpochSecond() on the Instant object to get the Unix time value of the specified time.

Finally, we assert that the return Unix time value is equal to the expected Unix time timestamp.

4. Using Joda-Time Library

The Joda-Time library provides a DateTime class to get the current time. After getting the current time, we can easily compute the Unix time. To use Joda Time, let’s add it’s dependency to the pom.xml:

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.12.5</version>
</dependency>

Here’s an example code that uses the DateTime class from the Joda-Time library to retrieve a Unix time:

@Test
void givenTimeUsingJodaTime_whenConvertedToUnixTime_thenMatch() {
    DateTime dateTime = new DateTime(2023, 2, 15, 00, 00, 00, 0);
    long expected = 1676419200;
    long actual = dateTime.getMillis() / 1000L;
    assertEquals(expected, actual);
}

In the code above, we create an instance of DateTime with a fixed date and time and invoke the getMillis() method. Next, we divide it by 1000L to get the Unix time timestamp.

5. Avoiding the Year 2038 Problem

Typically, Unix time is stored in signed 32-bit integers in many systems and programming languages. However, the maximum value that can be stored in a 32-bit integer is 2 147 483 647.

This creates an issue for Unix time because, at 03:14:07 UTC on 19 January 2038, the Unix time value will reach its limit. The next seconds will roll over to a negative number which may cause faulty behavior in a system, application failures, crashes, and malfunction of a system.

We can avoid this limitation by storing the Unix time in 64-bit long integers rather than 32-bit integers in Java.

6. Conclusion

In this article, we learned how to use the legacy Data API, the Date Time API, and the Joda-Time library to retrieve Unix time. Storing the Unix time value in 64-bit long integers avoids any limitations or overflow issues for future dates.

As always, the complete source code for the examples 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 closed on this article!