java - How to round DateTime of Joda library to the nearest X minutes? -


how round datetime of joda library nearest x minutes ?
example:

x = 10 minutes jun 27, 11:32 -> jun 27, 11:30 jun 27, 11:33 -> jun 27, 11:30 jun 27, 11:34 -> jun 27, 11:30 jun 27, 11:35 -> jun 27, 11:40 jun 27, 11:36 -> jun 27, 11:40 jun 27, 11:37 -> jun 27, 11:40 

the accepted answer doesn't correctly handle datetimes have seconds or milliseconds set. completeness, here's version handle correctly:

private datetime rounddate(final datetime datetime, final int minutes) {     if (minutes < 1 || 60 % minutes != 0) {         throw new illegalargumentexception("minutes must factor of 60");     }      final datetime hour = datetime.hourofday().roundfloorcopy();     final long millissincehour = new duration(hour, datetime).getmillis();     final int roundedminutes = ((int)math.round(         millissincehour / 60000.0 / minutes)) * minutes;     return hour.plusminutes(roundedminutes); } 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -