Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW