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

后端 未结 6 1556
[愿得一人]
[愿得一人] 2020-12-28 14:42

How to round DateTime of Joda library to the nearest X minutes ?
For example:

X = 10 minutes
Jun 27, 11:32 -> Jun 27, 11         


        
6条回答
  •  长发绾君心
    2020-12-28 15:06

    When you want to round Joda DateTime the best solution IMHO is to use the built-in roundHalfCeilingCopy and roundHalfFloorCopy methods:

    DateTime dateTime = DateTime.now();
    DateTime newDateTime = dateTime.minuteOfHour().roundHalfCeilingCopy();
    

    Please note that roundHalfCeilingCopy will favor the ceiling if halfway. You can use roundHalfFloorCopy in order to favor the floor in case it's halfway.

提交回复
热议问题