Java uses LocalTime's withMinute to round up/down by half an hour

Round down
08:00——》08:00
08:01——》08:00
08:29——》08:00
08:30——》08:30
08:31——》08:30
08 :59——》08:30
Then we can use the value after rounding down and add 30 minutes to get the time period.
08:00——》08:30
08:01——》08:30
08:29——》08:30
08:30——》09:00
08:31——》09:00
08:59—— 》09:00
DateTimeFormatter is thread-safe and can be placed in global variables.

		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm");

        LocalTime currentTime = LocalTime.parse("08:59", dateTimeFormatter);
        //按30分钟向下取整,返回:08:30
        LocalTime foodTime = currentTime.withMinute(currentTime.getMinute() / 30 * 30);
        //返回时间+30分钟的时间,返回:09:00
        LocalTime ceilTime = foodTime.plusMinutes(30L);
        //返回08:30-09:00
        System.out.println(foodTime + "-" + ceilTime);

Guess you like

Origin blog.csdn.net/weixin_43933728/article/details/131868979