Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between System.currentTimeMillis() and Date getTime()?

Tags:

java

I was hoping to squeeze a tiny performance gain out of many calls to a function that returns a timestamp. The function looks like this:

public static long get_now_ms(){
    // returns number of MILLISECONDS since epoch
    java.util.Date d = new java.util.Date();
    return d.getTime();
}

Can I just replace this with:

public static long get_now_ms(){
    // returns number of MILLISECONDS since epoch
    return System.currentTimeMillis();
}

I know that Date internally uses System.currentTimeMillis(). My question is more whether or not daylight savings time or time zone could ever lead to a difference in result with these two approaches. I imagine this may come up with Calendar objects, but not Date objects, but would love some clarification on this.

I know I will likely not see an appreciable difference in performance in a real-world application, but would nevertheless like to know the answer.

Thanks!

like image 785
DivideByHero Avatar asked Jun 08 '13 21:06

DivideByHero


People also ask

What does Date () getTime () do in Java?

Java Date getTime() Method The getTime() method of Java Date class returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by Date object.

What is System currentTimeMillis ()?

currentTimeMillis() method returns the current time in milliseconds. The unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

Which class can be used instead of system Getcurrent Timemillis () to get a date and time in Java?

Instances of this class are used to find the current instant, which can be interpreted using the stored time-zone to find the current date and time. As such, a clock can be used instead of System. currentTimeMillis() and TimeZone. getDefault() .

Is system currentTimeMillis accurate?

currentTimeMillis() actually give the time accurate to the nearest millisecond on Linux, Mac OS and Windows (and since which versions - I know, for example, that Windows only used to be accurate to the nearest 15/16 milliseconds).


2 Answers

No difference, except for the very slight lag caused by allocating a Date object.

From the javadoc the the default constructor of Date:

Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

A Date is just a thin wrapper around the epoch milliseconds, without any concept of timezones. Only when rendered to a String is timezone considered, but that is handled by the Locale class.

like image 68
Bohemian Avatar answered Oct 13 '22 18:10

Bohemian


I would suggest running a unit test (ex. https://gist.github.com/ledlogic/8532028). I saw only a slight overall benefit to running the System.currentTimeMillis versus the (new Date()).getTime().

1 billion runs: (1000 outer loops, 1,000,000 inner loops):
    System.currentTimeMillis(): 14.353 seconds
    (new Date()).getTime(): 16.668 seconds

Individual runs would sometimes be slightly biased toward the later approach - depending on your system activity.

like image 40
ledlogic Avatar answered Oct 13 '22 16:10

ledlogic