Question

I use for example this code to check if the user can do some action. So the user can only do one action each 5 seconds.

if((System.currentTimeMillis() - lastTime) > 5000)
{
    // Message: Ok, you can do action now.
}else{
    // Message: Have to wait 5 seconds to do action.
    return;
}

lastTime = System.currentTimeMillis();

But as we all know, System.currentTimeMillis() returns a long, and that long can keep increasing until it turns negativ..

My code should run on a server that need to have more than 1 month of uptime. So I'm afraid at some point System.currentTimeMillis() will return a negativ value and my code will always tell the user that he need to wait 5 seconds or the opposite.

I'm having real hard time to concentrate on this piece of code and fix it, so I'm asking you guys if you have a tip on how to fix this problem and make my code 100% safe.

Was it helpful?

Solution

Don't worry about it.

You know whos problem it is?

The guy who will need to update it on Sun Aug 17 03:12:55 GMT-04:00 292278994.

OTHER TIPS

A long in milliseconds can represent 292 277 266 years. I'm not sure this is the kind of thing you need to be worried about.

According to this thread, it will overflow in year 292278994. I will say it is plenty of time:)

As everyone has said don't worry about it but for future reference maybe you'd prefer to use Joda-Time to ask this kind of question.

import org.joda.time.DateTime;

if(lastTime.plusSeconds(5).isAfterNow()) {
    // Message: Ok, you can do action now.
}
else {
    // Message: Have to wait 5 seconds to do action.
    return;
}

lastTime = new DateTime();

System.currentTimeMillis() returns the time in milliseconds, between the current time and midnight, January 1, 1970 UTC. With the largest maximum value that can be represented as a long is 9,223,372,036,854,775,807, if my calculation is right (long max / (1000 * 3600 * 24 * 365)), that could go up to more than 292471208 years. If your program can survive that long, let someone who will be born that many years later worry about it like we did for Y2K.

Even though the time it will overflow is far, far into the future as others have stated. It won't even be a problem then because you are taking the difference of two times. e.g. say you take the year 292,278,994 and the year 292,278,995 (which would appear to be negative), the difference is only 1 year (a positive number) e.g. if you take

long overflowYear = Long.MIN_VALUE; // overvflow of Long.MAX_VALUE + 1
long okayYear = Long.MAX_VALUE;
// time = 1 (positive due to an underflow!)
long time = overflowYear - okayYear; 

This sort of this could happen with System.nanoTime() as it doesn't have a defined starting time and ticks one million time faster. However as long as you take the time difference, it doesn't matter if one is negative or positive provided they are less than 292 years apart.

So in answer to your question, even after the year 292,278,994 you won't have a problem until the application have been running for more than 292,278,994 years between calls to System.currentTimeMillis() !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top