Introduction
Java 8 introduced the java.time package, which provides a modern and comprehensive framework for handling date and time. However, there are still many scenarios where you need to get the current time in milliseconds, such as when measuring elapsed time or interacting with systems that use timestamps. Java provides several ways to get the current time in milliseconds, both with the new java.time package and with legacy methods.
In this guide, we’ll explore how to get the current time in milliseconds using both the modern java.time API and the legacy System class.
Table of Contents
- Problem Statement
- Solution Steps
- Java Program
- Get the Current Time in Milliseconds Using
System.currentTimeMillis() - Get the Current Time in Milliseconds Using
Instant.now()
- Get the Current Time in Milliseconds Using
- Advanced Considerations
- Conclusion
Problem Statement
The task is to create a Java program that:
- Retrieves the current time in milliseconds.
Example:
- Output: A
longvalue representing the current time in milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT).
Solution Steps
- Use
System.currentTimeMillis(): This is the traditional method to get the current time in milliseconds. - Use
Instant.now(): This method from thejava.timepackage can also be used to get the current time in milliseconds.
Java Program
Get the Current Time in Milliseconds Using System.currentTimeMillis()
The simplest and most direct way to get the current time in milliseconds is to use the System.currentTimeMillis() method.
/**
* Java 8 - Get the Current Time in Milliseconds using System.currentTimeMillis()
* Author: https://www.rameshfadatare.com/
*/
public class CurrentTimeMillisExample {
public static void main(String[] args) {
// Get the current time in milliseconds
long currentTimeMillis = System.currentTimeMillis();
// Display the current time in milliseconds
System.out.println("Current time in milliseconds: " + currentTimeMillis);
}
}
Output
Current time in milliseconds: 1693239045123
Get the Current Time in Milliseconds Using Instant.now()
Java 8’s java.time.Instant class provides another way to get the current time in milliseconds.
import java.time.Instant;
/**
* Java 8 - Get the Current Time in Milliseconds using Instant.now()
* Author: https://www.rameshfadatare.com/
*/
public class CurrentTimeMillisUsingInstant {
public static void main(String[] args) {
// Get the current time in milliseconds using Instant
long currentTimeMillis = Instant.now().toEpochMilli();
// Display the current time in milliseconds
System.out.println("Current time in milliseconds: " + currentTimeMillis);
}
}
Output
Current time in milliseconds: 1693239045123
Explanation
- System.currentTimeMillis(): This method returns the current time in milliseconds since the Unix epoch. It’s a simple and direct approach and has been in use since the early versions of Java.
- Instant.now().toEpochMilli(): This method uses the
Instantclass, which represents a point in time. ThetoEpochMilli()method converts theInstantto milliseconds since the Unix epoch. This is part of the new date and time API introduced in Java 8.
Advanced Considerations
-
Precision: While both methods provide the time in milliseconds, the
Instantclass supports nanosecond precision internally. However, when you calltoEpochMilli(), the result is truncated to milliseconds. -
Time Zones: The milliseconds returned by both methods are relative to UTC (Coordinated Universal Time), making them suitable for time-sensitive operations like logging or measuring elapsed time.
-
Performance:
System.currentTimeMillis()is a native call and is generally very fast. TheInstant.now()approach, while also efficient, involves more steps under the hood, but it provides more flexibility within thejava.timeAPI.
Conclusion
This guide covers how to get the current time in milliseconds using both the traditional System.currentTimeMillis() method and the modern Instant.now().toEpochMilli() method from Java 8’s java.time package. Depending on your use case, you can choose the method that best fits your needs, whether you need simplicity or are working within the broader context of the java.time API. Both methods are effective and provide accurate millisecond precision for your Java applications.