Convert Time to Milliseconds in Java

1. Overview

In this quick tutorial, we’ll illustrate multiple ways of converting time into Unix-epoch milliseconds in Java.

More specifically, we’ll use:

  • Core Java’s java.util.Date and Calendar
  • Java 8’s Date and Time API
  • Joda-Time library

2. Core Java

2.1. Using Date

Firstly, let’s define a millis property holding a random value of milliseconds:

long millis = 1556175797428L; // April 25, 2019 7:03:17.428 UTC

We’ll use this value to initialize our various objects and verify our results.

Next, let’s start with a Date object:

Date date = // implementation details

Now, we’re ready to convert the date into milliseconds by simply invoking the getTime() method:

Assert.assertEquals(millis, date.getTime());

2.2. Using Calendar

Likewise, if we have a Calendar object, we can use the getTimeInMillis() method:

Calendar calendar = // implementation details
Assert.assertEquals(millis, calendar.getTimeInMillis());

3. Java 8 Date Time API

3.1. Using Instant

Simply put, Instant is a point in Java’s epoch timeline.

We can get the current time in milliseconds from the Instant:

java.time.Instant instant = // implementation details
Assert.assertEquals(millis, instant.toEpochMilli());

As a result, the toEpochMilli() method returns the same number of milliseconds as we defined earlier.

3.2. Using LocalDateTime

Similarly, we can use Java 8’s Date and Time API to convert a LocalDateTime into milliseconds:

LocalDateTime localDateTime = // implementation details
ZonedDateTime zdt = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
Assert.assertEquals(millis, zdt.toInstant().toEpochMilli());

First, we created an instance of the current date. After that, we used the toEpochMilli() method to convert the ZonedDateTime into milliseconds.

As we know, LocalDateTime doesn’t contain information about the time zone. In other words, we can’t get milliseconds directly from LocalDateTime instance.

4. Joda-Time

While Java 8 adds much of Joda-Time’s functionality, we may want to use this option if we are on Java 7 or earlier.

4.1. Using Instant

Firstly, we can obtain the current system milliseconds from the Joda-Time Instant class instance using the getMillis() method:

Instant jodaInstant = // implementation details
Assert.assertEquals(millis, jodaInstant.getMillis());

4.2. Using DateTime

Additionally, if we have a Joda-Time DateTime instance:

DateTime jodaDateTime = // implementation details

Then we can retrieve the milliseconds with the getMillis() method:

Assert.assertEquals(millis, jodaDateTime.getMillis());

5. Conclusion

In conclusion, this article demonstrates how to convert time into milliseconds in Java.

Finally, as always, the complete code for this article is available over on GitHub.

Related posts:

Python Program to Convert String to Datetime
Custom Thread Pools In Java 8 Parallel Streams
Spring Security Authentication Provider
Java Program to Implement Gaussian Elimination Algorithm
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Decode a Message Encoded Using Playfair Cipher
Creating a Generic Array in Java
Java Program to Implement Rope
Java Program to Implement Graph Coloring Algorithm
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
REST Web service: Basic Authentication trong Jersey 2.x
Jackson – Bidirectional Relationships
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Lập trình đa luồng với CompletableFuture trong Java 8
Tổng quan về ngôn ngữ lập trình java
Xử lý ngoại lệ trong Java (Exception Handling)
Java 9 Stream API Improvements
Truyền giá trị và tham chiếu trong java
wait() and notify() Methods in Java
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Creating a Web Application with Spring 5
Toán tử instanceof trong java
Java Program to Check whether Undirected Graph is Connected using DFS
Spring Boot - Google Cloud Platform
Spring RequestMapping
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Java Program to Implement Euler Circuit Problem
Java Program to Implement Doubly Linked List
Build a REST API with Spring and Java Config
Java Program to Create a Random Graph Using Random Edge Generation
Mapping Nested Values with Jackson
Introduction to Java 8 Streams