1

This question already has an answer here:

I just stumbled accross this method

public static Date getNowDate() {
    final Calendar cal = new GregorianCalendar();
    cal.setTime(new Date());
    return cal.getTime();
}

which gets called like this:

getNowDate().getTime()

Is this any different from just calling

System.currentTimeMillis()

?


3 답변


2

They are all much the same except for performance.

System.currentTimeMillis() is a system call so it takes around 0.1 to 0.3 micro-seconds (depending on your OS)

new Date() also creates an object, which takes only about 0.1 to 0.3 micro-seconds more but creates a little garbage.

Calendar.getInstance() creates an expensive set of objects and takes about 33 micro-seconds more.


3

No difference in terms of resultant return value of long (millis)

If you wish current date, simpler way would be, You are setting the date in Calendar and retrieving it back in getNowDate() which can be simplify as follow

public static Date getNowDate() {
  return new Date();
}


  • I wonder if there is a difference in means of efficiency. - overbet13

0

I don't think there is any difference between these two, in the form of result, as both of them will return long values dating back from January 1st 1970 till date.

Even simply calling Date d = new Date(); will give you the present date and calling d.getTime(); will return its long repersentation

Linked


Related

Latest