Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, what is the fastest way to get the system time?

I'm developing a system that often use the system time because the Delayed interface.

What is fastet way to get the time from system?

Currently I'm using Calendar.getInstance().getTimeInMillis() every time I need to get the time, but I don't know if there is a faster way.

like image 253
Renato Dinhani Avatar asked Jul 01 '11 19:07

Renato Dinhani


People also ask

How to get current time of System in java?

You can get the time from the LocaldateTime object using the toLocalTime() method. Therefore, another way to get the current time is to retrieve the current LocaldateTime object using the of() method of the same class. From this object get the time using the toLocalTime() method.

How Fast Is system currentTimeMillis?

System. currentTimeMillis() takes about 29 nanoseconds per call while System. nanoTime() takes about 25 nanoseconds.

How to get time from Datetime in java?

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. Parameters: The function does not accept any parameter. Return Value: It returns the number of milliseconds since January 1, 1970, 00:00:00 GTM.

How do you subtract time in Java?

We need to use the minus methods listed above. LocalDateTime updatedTime LocalDateTime now = LocalDateTime. now(); updatedTime = now. minusHours(2); updatedTime = now.


1 Answers

System.currentTimeMillis()
"Returns the current time in milliseconds".
Use this to get the actual system time.

System.nanoTime().
"The value returned represents nanoseconds since some fixed but arbitrary origin time"
Use this is you're measuring time lapses / events.

like image 128
Bozho Avatar answered Oct 12 '22 20:10

Bozho