How to round DateTime
of Joda
library to the nearest X
minutes ?
For example:
X = 10 minutes Jun 27, 11:32 -> Jun 27, 11
Here's another approach that uses arithmetic on Unix time for completeness:
(Implemented in Scala for clarity.)
import org.joda.time.{DateTime, Duration}
def roundDateTime(t: DateTime, d: Duration) = {
t minus (t.getMillis - (t.getMillis.toDouble / d.getMillis).round * d.getMillis)
}
Example usage:
roundDateTime(new DateTime("2013-06-27T11:32:00"), Duration.standardMinutes(10))
// => 2013-06-27T11:30:00.000+02:00
roundDateTime(new DateTime("2013-06-27T11:37:00"), Duration.standardMinutes(10))
// => 2013-06-27T11:40:00.000+02:00