I need a fast implementation of a "Date jump" algorithm that is to be included in an event management system.
An event is triggered and sets a date (in a synchronized method) to the next 10th minute.
For instance
Event occurs at "2010-01-05 13:10:12" and sets the
next date to be "2010-01-05 13:20:00"
and if an event occurs exactly (supposedly) at a 10th minute, the next one must be set
Event occurs at "2010-01-05 13:30:00" and sets the
next date to be "2010-01-05 13:40:00"
(unlikely since the date goes down to the 1/1000th of a second, but just in case...).
My first idea would be to get the current Date()
and work directly with the ms
from the getTime() method, via integer (long) division, like ((time / 10mn)+1)*10mn
.
Since it has to be fast, and also reliable, I thought I'll ask my fellow OSers prior to the implementation.
You can use / adapt my answer to a very similar question:
How to round time to the nearest quarter in java?
Something like this:
int unroundedMinutes = calendar.get(Calendar.MINUTE);
int mod = unroundedMinutes % 10;
calendar.add(Calendar.MINUTE, mod == 0 ? 10 : 10 - mod);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With