实现说明
底层就是类 NightShiftClock和函数 main。
使用要点
- 直接运行文件,输出会带上标签,比如
shift start :、+90 minutes :、0:30 minus 2h:——输出的每一行都能指回产生它的那次调用。
常见问题
运行「LocalTime 跨午夜滚动运算」会打印什么?
文件本身就是完整程序,直接运行——输出行带有 shift start :、+90 minutes :、0:30 minus 2h: 这类标签。
核心语句
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);
代码示例
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)));
}
}