Marlon López Marlon López - 1 month ago 16
Java Question

Java current time different values in api

I'm a little confused using the DateTime related class for Java SE 7 and 8 API's, for displaying the current time, I'm reviewing the multiple ways for get the system's current datetime.

My question is: Which one is more accurate for displaying time in millis?

Next is the code snippet, I'm using Java 8 for the reviewing.

import java.time.Instant;
import java.util.Calendar;
import java.util.Date;

public class CurrentTimeValidationDemo {
public static void main(String[] args) {
Instant now =;
Calendar calendar=Calendar.getInstance();
Date currDate = new Date();
System.out.println("new Date().getTime() = "+currDate.getTime());
System.out.println("System.currentTimeMillis() = "+System.currentTimeMillis());
System.out.println(" = "+now.toEpochMilli());
System.out.println("Calendar.getInstance().getTimeInMillis() = "+calendar.getTimeInMillis());
System.out.println("Calendar.getInstance().getTime().getTime() = "+calendar.getTime().getTime());

Answer Source

All the functions that you used in your example return the same value (if launched in the same millisecond) none of them is more accurate.

Once of them is not creating any object, so if you need only to know the current milliseconds since 1/1/1970 use


Instead if you need to have also the equivalent object to store that value or to make additional operations use the object that you need.

For example if you need to pass this value to a function accepting a java.util.Date use java.util.Date (and so on).