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

后端 未结 14 2674
小鲜肉
小鲜肉 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:22

    You can use this simple code...

    int mode = min % 15;
    if (mode > 15 / 2) {
        min = 15 - mode;
    } else {
        min = 0 - mode;
    }
    cal.add(Calendar.MINUTE, min);
    

提交回复
热议问题