Go to the Java date until the next 10th minute

I need a quick implementation of the "Date Transition" algorithm, which should be included in the event management system.
The event is triggered and sets the date (in a synchronized manner) 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 the event occurs exactly (presumably) in the 10th minute, the next should 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, as the date drops to 1 / 1000th of a second, but just in case ...).

My first idea would be to get the current Date() and work directly with ms from the getTime () method using integer (long) division, e.g. ((time / 10mn)+1)*10mn .

Since it should be fast as well as reliable, I thought I would ask my OSers assistants before implementation.

+4
source share
2 answers

You can use / customize your answer to a very similar question:

How to round time to the nearest quarter in java?

Something like that:

 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); 
+9
source

Your approach with an era time in ms will not work for arbitrary time zones, as some time zones have an N * 15 minute offset from GMT. During these time zones, your code will move the interval 5-14 to 15, 15-24 to 25, etc.

Have you really tested the performance of manipulating the corresponding fields of the GregorianCalendar class and came to the conclusion that the performance is insufficient or are you trying to invent a wheel just for fun?

0
source

Source: https://habr.com/ru/post/1334399/


All Articles