Java 时间日期
在 Java 8 之前,处理日期和时间主要依赖 java.util.Date 和 java.util.Calendar 类,它们存在设计缺陷、线程不安全、API 混乱等问题。从 Java 8 开始,引入了全新的 java.time 包,它提供了不可变、线程安全且设计清晰的 API,是现代 Java 开发中处理时间日期的标准方式。
java.time 包的核心类
LocalDate: 表示一个不带时区的日期(年-月-日)。例如:2024-07-25。LocalTime: 表示一个不带时区的时间(时:分:秒.纳秒)。例如:15:30:00。LocalDateTime: 表示日期和时间的组合,不带时区。例如:2024-07-25T15:30:00。ZonedDateTime: 表示一个带时区的日期和时间。这是处理需要考虑时区转换的场景时的首选。Instant: 表示时间线上的一个瞬时点(时间戳),通常用于机器之间的时间记录,内部以 UTC 时区为基准。DateTimeFormatter: 用于格式化和解析日期时间对象。
创建日期和时间对象
获取当前日期和时间非常简单。
java
import java.time.*;
public class DateTimeCreation {
public static void main(String[] args) {
// 获取当前日期
LocalDate today = LocalDate.now();
System.out.println("今天的日期: " + today);
// 获取当前时间
LocalTime now = LocalTime.now();
System.out.println("当前的时间: " + now);
// 获取当前日期和时间
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("当前的日期和时间: " + currentDateTime);
// 获取带时区的当前日期和时间
ZonedDateTime zonedNow = ZonedDateTime.now();
System.out.println("带时区的当前时间: " + zonedNow);
}
}您也可以使用 of() 方法创建指定的日期和时间。
java
// 创建指定日期: 2025年1月1日
LocalDate specificDate = LocalDate.of(2025, 1, 1);
// 创建指定时间: 10点15分30秒
LocalTime specificTime = LocalTime.of(10, 15, 30);
// 创建指定日期时间
LocalDateTime specificDateTime = LocalDateTime.of(specificDate, specificTime);
System.out.println("指定的日期时间: " + specificDateTime);操作日期和时间
java.time API 的所有对象都是不可变的,任何修改操作都会返回一个新的实例。
java
LocalDate today = LocalDate.now();
// 加法
LocalDate nextWeek = today.plusWeeks(1);
LocalDate tomorrow = today.plusDays(1);
// 减法
LocalDate yesterday = today.minusDays(1);
// 修改特定字段
LocalDate firstDayOfMonth = today.withDayOfMonth(1);
System.out.println("明天是: " + tomorrow);
System.out.println("本月第一天是: " + firstDayOfMonth);获取日期时间信息
可以轻松地从日期时间对象中提取所需的部分。
java
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
Month month = now.getMonth(); // 返回一个 Month 枚举
int day = now.getDayOfMonth();
DayOfWeek dayOfWeek = now.getDayOfWeek(); // 返回一个 DayOfWeek 枚举
System.out.println("年份: " + year);
System.out.println("月份: " + month.getValue()); // 获取月份的数字值
System.out.println("今天是: " + dayOfWeek);格式化和解析
DateTimeFormatter 是格式化和解析日期时间的核心工具。
java
import java.time.format.DateTimeFormatter;
LocalDateTime now = LocalDateTime.now();
// 1. 定义格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
// 2. 格式化:将 LocalDateTime 转换为字符串
String formattedDateTime = now.format(formatter);
System.out.println("格式化后: " + formattedDateTime);
// 3. 解析:将字符串转换回 LocalDateTime
String dateTimeStr = "2025年01月01日 08:00:00";
LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeStr, formatter);
System.out.println("解析后: " + parsedDateTime);Java 也提供了一些预定义的格式化器,如 DateTimeFormatter.ISO_LOCAL_DATE_TIME。
计算时间差
Period: 用于计算两个日期 (LocalDate) 之间的差值,单位是年、月、日。Duration: 用于计算两个时间点 (LocalTime,LocalDateTime,Instant) 之间的差值,单位是天、时、分、秒、纳秒。
java
// Period 示例
LocalDate startDate = LocalDate.of(2024, 1, 1);
LocalDate endDate = LocalDate.of(2025, 3, 15);
Period period = Period.between(startDate, endDate);
System.out.printf("相差: %d年, %d月, %d日\n", period.getYears(), period.getMonths(), period.getDays());
// Duration 示例
LocalTime startTime = LocalTime.of(9, 0);
LocalTime endTime = LocalTime.of(10, 30, 15);
Duration duration = Duration.between(startTime, endTime);
System.out.println("相差秒数: " + duration.getSeconds());