比如预约网约车的时候,是按5分钟的整点时间
@GetMapping("/getFiveNextTime")
public String fiveNextTime(@RequestParam(defaultValue = "0") Integer interval) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, (5 - calendar.get(Calendar.MINUTE) % 5) + interval.intValue() * 5);
calendar.set(Calendar.SECOND,0);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long nowTime = calendar.getTimeInMillis();
String timeNextTime = simpleDateFormat.format(nowTime);
// System.out.println(timeNextTime);
return timeNextTime;
}
测试结果 如当前时间是2024-02-04 11:14
如果入参为空时:获取的时间是2024-02-04 11:15:00
如果interval=2返回的是2024-02-04 11:25:00
就是11点15分再加2个五分钟,就是11:25