Java LocalTime 类
LocalTime
是 Java 8 引入的日期时间 API (java.time
包)中的一个重要类,用于表示不带时区信息的时间。它专门处理一天中的时间,精确到纳秒级别。
LocalTime 类的特点
1. 不可变性
LocalTime 是不可变类,所有修改操作都会返回新的实例,原始对象保持不变。
2. 无时区信息
LocalTime 不包含时区信息,仅表示本地时间。
3. 时间精度
支持从小时到纳秒的时间精度(HH:mm:ss.nnnnnnnnn)。
创建 LocalTime 对象
1. 获取当前时间
LocalTime currentTime = LocalTime.now(); System.out.println("当前时间: " + currentTime);
2. 使用 of() 方法创建
实例
// 创建 14:30:00
LocalTime time1 = LocalTime.of(14, 30);
// 创建 09:15:30
LocalTime time2 = LocalTime.of(9, 15, 30);
// 创建 10:20:30.500 (500毫秒)
LocalTime time3 = LocalTime.of(10, 20, 30, 500000000);
LocalTime time1 = LocalTime.of(14, 30);
// 创建 09:15:30
LocalTime time2 = LocalTime.of(9, 15, 30);
// 创建 10:20:30.500 (500毫秒)
LocalTime time3 = LocalTime.of(10, 20, 30, 500000000);
3. 解析字符串
LocalTime parsedTime = LocalTime.parse("08:45"); System.out.println("解析的时间: " + parsedTime);
常用方法
1. 获取时间分量
实例
LocalTime time = LocalTime.of(15, 45, 20, 123456789);
<p>int hour = time.getHour(); // 15
int minute = time.getMinute(); // 45
int second = time.getSecond(); // 20
int nano = time.getNano(); // 123456789
<p>int hour = time.getHour(); // 15
int minute = time.getMinute(); // 45
int second = time.getSecond(); // 20
int nano = time.getNano(); // 123456789
2. 时间比较
实例
LocalTime time1 = LocalTime.of(10, 30);
LocalTime time2 = LocalTime.of(11, 15);
boolean isBefore = time1.isBefore(time2); // true
boolean isAfter = time1.isAfter(time2); // false
boolean isEqual = time1.equals(time2); // false
LocalTime time2 = LocalTime.of(11, 15);
boolean isBefore = time1.isBefore(time2); // true
boolean isAfter = time1.isAfter(time2); // false
boolean isEqual = time1.equals(time2); // false
3. 时间运算
实例
LocalTime time = LocalTime.of(9, 0);
// 加2小时
LocalTime plusHours = time.plusHours(2); // 11:00
// 减30分钟
LocalTime minusMinutes = time.minusMinutes(30); // 08:30
// 加1小时15分钟
LocalTime plusDuration = time.plus(Duration.ofMinutes(75)); // 10:15
// 加2小时
LocalTime plusHours = time.plusHours(2); // 11:00
// 减30分钟
LocalTime minusMinutes = time.minusMinutes(30); // 08:30
// 加1小时15分钟
LocalTime plusDuration = time.plus(Duration.ofMinutes(75)); // 10:15
4. 时间调整
实例
LocalTime time = LocalTime.of(14, 30);
// 设置小时为10
LocalTime withHour = time.withHour(10); // 10:30
// 设置分钟为45
LocalTime withMinute = time.withMinute(45); // 14:45
// 设置小时为10
LocalTime withHour = time.withHour(10); // 10:30
// 设置分钟为45
LocalTime withMinute = time.withMinute(45); // 14:45
格式化与解析
1. 格式化时间
实例
java LocalTime time = LocalTime.of(16, 45, 30);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String formattedTime = time.format(formatter); // "16:45:30"
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String formattedTime = time.format(formatter); // "16:45:30"
2. 解析时间字符串
实例
String timeString = "08:15:45 AM";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
LocalTime parsedTime = LocalTime.parse(timeString, formatter);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
LocalTime parsedTime = LocalTime.parse(timeString, formatter);
实际应用示例
1. 计算两个时间点之间的间隔
实例
LocalTime start = LocalTime.of(9, 0);
LocalTime end = LocalTime.of(17, 30);
Duration duration = Duration.between(start, end);
System.out.println("工作时间: " + duration.toHours() + " 小时"); // 8 小时
LocalTime end = LocalTime.of(17, 30);
Duration duration = Duration.between(start, end);
System.out.println("工作时间: " + duration.toHours() + " 小时"); // 8 小时
2. 检查是否在营业时间内
实例
LocalTime current = LocalTime.now();
LocalTime opening = LocalTime.of(8, 0);
LocalTime closing = LocalTime.of(22, 0);
if (current.isAfter(opening) && current.isBefore(closing)) {
System.out.println("商店正在营业");
} else {
System.out.println("商店已关门");
}
LocalTime opening = LocalTime.of(8, 0);
LocalTime closing = LocalTime.of(22, 0);
if (current.isAfter(opening) && current.isBefore(closing)) {
System.out.println("商店正在营业");
} else {
System.out.println("商店已关门");
}
3. 创建时间范围
实例
LocalTime start = LocalTime.of(8, 0);
LocalTime end = start.plusHours(8); // 16:00
System.out.println("工作时间: " + start + " 到 " + end);
LocalTime end = start.plusHours(8); // 16:00
System.out.println("工作时间: " + start + " 到 " + end);
总结
LocalTime
类是 Java 8 日期时间 API 中处理本地时间的核心类,它提供了丰富的方法来创建、操作和格式化时间。由于其不可变性和线程安全性,LocalTime
非常适合在多线程环境中使用。掌握 LocalTime
类的使用可以大大简化时间相关的编程任务。
点我分享笔记