希望你能提供帮助: - )
挣扎着一个不寻常的舍入问题......
如果时间是“早上7:35”,那么时间应该是“上午8:00” - 四舍五入到最接近的30分钟......
如果时间是“早上7:20”,那么时间应该是“早上7:30”
任何想法都会受到赞赏。
我有这个,但它向前和向后转。只需要它继续前进:
private DateTime roundDate(final DateTime dateTime, final int minutes) {
if (minutes < 1 || 60 % minutes != 0)
{
throw new IllegalArgumentException("minutes must be a 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;
System.out.println(hour.plusMinutes(roundedMinutes));
return hour.plusMinutes(roundedMinutes);
}