How to round time to the nearest quarter hour in java?

后端 未结 14 2727
小鲜肉
小鲜肉 2020-11-27 14:59

Given today\'s time e.g. 2:24PM, how do I get it to round to 2:30PM?

Similarly if the time was 2:17PM, how do I get it to round to 2:15PM?

14条回答
  •  天涯浪人
    2020-11-27 15:33

    if you have the minutes you can round them with the following function:
    int minutes = i % 15 < 8 ? i / 15 * 15 : (i / 15 + 1) * 15;

提交回复
热议问题