Java时间API终极指南

Java 时间 API 概述

Java 提供了多个时间相关的 API,主要包括 java.util.Datejava.util.Calendarjava.time 包(Java 8 引入)。其中,java.time 是最现代且功能最全面的时间 API。以下分为新旧 API 分别介绍。


java.time 包(Java 8+ 推荐使用)

java.time 包分为多个类,包括 LocalDateLocalTimeLocalDateTimeZonedDateTime 等。

LocalDate(仅日期)
// 无参构造:获取当前日期
LocalDate today = LocalDate.now();
System.out.println("Today: " + today); // 输出:2023-10-05

// 有参构造:指定日期
LocalDate specificDate = LocalDate.of(2023, 10, 1);
System.out.println("Specific Date: " + specificDate); // 输出:2023-10-01

// 成员方法示例
LocalDate tomorrow = today.plusDays(1); // 增加 1 天
System.out.println("Tomorrow: " + tomorrow);

int year = today.getYear(); // 获取年份
System.out.println("Year: " + year);

LocalTime(仅时间)
// 无参构造:获取当前时间
LocalTime currentTime = LocalTime.now();
System.out.println("Current Time: " + currentTime); // 输出:14:30:15.123

// 有参构造:指定时间
LocalTime specificTime = LocalTime.of(12, 30, 45);
System.out.println("Specific Time: " + specificTime); // 输出:12:30:45

// 成员方法示例
LocalTime laterTime = currentTime.plusHours(2); // 增加 2 小时
System.out.println("Later Time: " + laterTime);

int hour = specificTime.getHour(); // 获取小时
System.out.println("Hour: " + hour);

LocalDateTime(日期 + 时间)
// 无参构造:获取当前日期和时间
LocalDateTime now = LocalDateTime.now();
System.out.println("Now: " + now); // 输出:2023-10-05T14:30:15.123

// 有参构造:指定日期时间
LocalDateTime specificDateTime = LocalDateTime.of(2023, 10, 1, 12, 30);
System.out.println("Specific DateTime: " + specificDateTime); // 输出:2023-10-01T12:30

// 成员方法示例
LocalDateTime nextMonth = now.plusMonths(1); // 增加 1 个月
System.out.println("Next Month: " + nextMonth);

int dayOfMonth = specificDateTime.getDayOfMonth(); // 获取日
System.out.println("Day: " + dayOfMonth);

ZonedDateTime(带时区的日期时间)
// 无参构造:获取当前时区的日期时间
ZonedDateTime zonedNow = ZonedDateTime.now();
System.out.println("Zoned Now: " + zonedNow); // 输出:2023-10-05T14:30:15.123+08:00[Asia/Shanghai]

// 有参构造:指定时区
ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime newYorkTime = ZonedDateTime.now(zoneId);
System.out.println("New York Time: " + newYorkTime); // 输出:2023-10-05T02:30:15.123-04:00[America/New_York]

// 成员方法示例
ZoneId currentZone = zonedNow.getZone(); // 获取时区
System.out.println("Current Zone: " + currentZone);


java.util.Date(旧 API,不推荐)

// 无参构造:当前时间
Date currentDate = new Date();
System.out.println("Current Date: " + currentDate); // 输出:Thu Oct 05 14:30:15 CST 2023

// 有参构造:指定时间(毫秒时间戳)
Date specificDate = new Date(1696491000000L);
System.out.println("Specific Date: " + specificDate); // 输出:Wed Oct 04 00:00:00 CST 2023

// 成员方法示例
long timeInMillis = currentDate.getTime(); // 获取毫秒时间戳
System.out.println("Time in Millis: " + timeInMillis);


java.util.Calendar(旧 API,不推荐)

// 无参构造:当前时间
Calendar calendar = Calendar.getInstance();
System.out.println("Calendar Time: " + calendar.getTime()); // 输出:Thu Oct 05 14:30:15 CST 2023

// 有参构造:指定日期(需先获取实例再设置)
Calendar specificCalendar = Calendar.getInstance();
specificCalendar.set(2023, Calendar.OCTOBER, 1); // 注意月份从 0 开始
System.out.println("Specific Calendar: " + specificCalendar.getTime()); // 输出:Sun Oct 01 14:30:15 CST 2023

// 成员方法示例
int month = calendar.get(Calendar.MONTH); // 获取月份(0-11)
System.out.println("Month: " + month);

calendar.add(Calendar.DAY_OF_MONTH, 5); // 增加 5 天
System.out.println("After 5 Days: " + calendar.getTime());


总结

  • java.time(推荐):功能丰富,线程安全,适用于现代 Java 应用。
  • java.util.Date / Calendar(旧 API):存在设计缺陷,建议仅用于兼容旧代码。

使用时优先选择 java.time 包,代码更简洁、可读性更强。

你可能感兴趣的:(java,开发语言)