Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When will System.currentTimeMillis() overflow?

I have a web app which orders stuff using a timestamp, which is just a long. My web app backend happens to be written in java, so I am using:

long timestamp = System.currentTimeMillis(); 

what year (approximately) will this fail? I mean at some point, the range of a long is going to overflow, right? We may all be long-dead, but I'm just curious. Will it be like y2k all over again? What can I do to prepare for this? Ridiculous, I know, just curious!

like image 457
user246114 Avatar asked Jun 04 '10 23:06

user246114


People also ask

How accurate is system currentTimeMillis?

This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Depending on the system, it can take more than 100 cpu cycles to execute.

How Fast Is system currentTimeMillis?

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

How does system currentTimeMillis work?

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.

Is system currentTimeMillis expensive?

So in short, this is pretty cheap on its own.


2 Answers

It will overflow at

System.out.println(new Date(Long.MAX_VALUE)); 

which prints

Sun Aug 17 03:12:55 GMT-04:00 292278994 

That's thus after a bit more than 292 million years. I'd say, there's a plenty of time to invent a solution in the meanwhile. To be honest, I don't expect the humanhood to survive this. We exist only a few seconds as compared to the age of the world in hour scale and it won't take long.

enter image description here

like image 138
BalusC Avatar answered Oct 04 '22 03:10

BalusC


Try running this code:

System.out.println(new Date(Long.MAX_VALUE)); 

Which prints something like this depending on your locale:

Sun Aug 17 17:12:55 EST 292278994 

Very long in the future, so no need to worry about overflow.

like image 30
krock Avatar answered Oct 04 '22 03:10

krock