LocalTime Arithmetic Rolls Over Midnight

Shift times past midnight wrap inside LocalTime: plusMinutes on 23:30 lands on 01:00 next cycle, minusHours before 00:00 rolls back into the evening.

JavaData Integrity

Code

snippet
import java.time.LocalTime;

public class NightShiftClock {

    public static void main(String[] args) {
        LocalTime shiftStart = LocalTime.of(23, 30);

        // LocalTime 没有日期分量:加法越过 24:00 从 00:00 重新滚动
        LocalTime breakTime = shiftStart.plusMinutes(90); // 01:00,属于下一天的钟点
        System.out.println("shift start : " + shiftStart);
        System.out.println("+90 minutes : " + breakTime);

        // 00:30 减 2 小时滚回前一晚的 22:30
        LocalTime back = LocalTime.of(0, 30).minusHours(2);
        System.out.println("0:30 minus 2h: " + back);

        // MAX 加 1 纳秒绕回 MIN;MIN 就是 00:00
        System.out.println("max + 1ns   : " + LocalTime.MAX.plusNanos(1));
        System.out.println("min == 0:00 : " + LocalTime.MIN.equals(LocalTime.of(0, 0)));
    }
}

What happens when it runs

LocalTime Arithmetic Rolls Over Midnight is built around the class NightShiftClock and the function main.

Watch out for

  • The project needs nothing added before a run.

Using it

Signature: public static void main(String[] args)

The demo output is labeled (shift start :, +90 minutes :, 0:30 minus 2h:), so the printed lines each trace back to a specific operation.

Key statements

LocalTime breakTime = shiftStart.plusMinutes(90); // 01:00,属于下一天的钟点
System.out.println("shift start : " + shiftStart);
LocalTime back = LocalTime.of(0, 30).minusHours(2);
LocalTime shiftStart = LocalTime.of(23, 30);

Interactive Tools for This Topic

FAQ

What does LocalTime Arithmetic Rolls Over Midnight print when run?
Run it as a complete program — the output lines carry labels such as `shift start :`, `+90 minutes :`, `0:30 minus 2h:`.