日期时间 API
日期时间 API (java.time
包),旨在解决旧版 java.util.Date
和 java.util.Calendar
存在的一些设计缺陷,比如线程不安全、时区处理不一致等问题。新 API 基于 ISO 8601 标准,更加直观、简洁,且支持时区和区域设置。主要类有:LocalDate
、LocalTime
、LocalDateTime
、ZonedDateTime
、Instant
、Duration
、Period
等。
1. LocalDate:表示日期(无时区)
LocalDate
用于表示没有时间部分和时区的日期(如:年-月-日)。
创建 LocalDate 实例
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
// 当前日期
LocalDate today = LocalDate.now();
System.out.println("今天的日期: " + today);
// 指定日期
LocalDate specificDate = LocalDate.of(2025, 5, 15);
System.out.println("指定日期: " + specificDate);
// 从字符串创建
LocalDate fromString = LocalDate.parse("2025-05-15");
System.out.println("从字符串创建: " + fromString);
}
}
常用方法
now()
:获取当前日期。of(year, month, day)
:创建指定日期。plusDays(long days)
:增加天数。minusMonths(long months)
:减少月份。getDayOfWeek()
:获取星期几。
2. LocalTime:表示时间(无日期、无时区)
LocalTime
表示一天中的时间(小时、分钟、秒),不涉及日期和时区。
创建 LocalTime 实例
import java.time.LocalTime;
public class LocalTimeExample {
public static void main(String[] args) {
// 当前时间
LocalTime now = LocalTime.now();
System.out.println("当前时间: " + now);
// 指定时间
LocalTime specificTime = LocalTime.of(14, 30);
System.out.println("指定时间: " + specificTime);
// 从字符串创建
LocalTime fromString = LocalTime.parse("14:30:15");
System.out.println("从字符串创建: " + fromString);
}
}
常用方法
now()
:获取当前时间。of(hour, minute)
:创建指定时间。plusMinutes(long minutes)
:增加分钟。minusSeconds(long seconds)
:减少秒数。getHour()
:获取小时。
3. LocalDateTime:表示日期和时间(无时区)
LocalDateTime
结合了 LocalDate
和 LocalTime
,表示日期和时间,依然不涉及时区。
创建 LocalDateTime 实例
import java.time.LocalDateTime;
public class LocalDateTimeExample {
public static void main(String[] args) {
// 当前日期和时间
LocalDateTime now = LocalDateTime.now();
System.out.println("当前日期和时间: " + now);
// 指定日期和时间
LocalDateTime specificDateTime = LocalDateTime.of(2025, 5, 15, 14, 30);
System.out.println("指定日期和时间: " + specificDateTime);
// 从字符串创建
LocalDateTime fromString = LocalDateTime.parse("2025-05-15T14:30:00");
System.out.println("从字符串创建: " + fromString);
}
}
常用方法
now()
:获取当前日期和时间。of(year, month, day, hour, minute)
:创建指定日期和时间。plusHours(long hours)
:增加小时数。minusMinutes(long minutes)
:减少分钟数。getYear()
:获取年份。
4. ZonedDateTime:表示带时区的日期和时间
ZonedDateTime
表示带有时区的日期时间,它可以精确到不同的时区,适用于跨时区的场景。
创建 ZonedDateTime 实例
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
// 当前时区的当前时间
ZonedDateTime now = ZonedDateTime.now();
System.out.println("当前时区时间: " + now);
// 指定时区的当前时间
ZonedDateTime timeInLondon = ZonedDateTime.now(ZoneId.of("Europe/London"));
System.out.println("伦敦的当前时间: " + timeInLondon);
// 创建指定时间和时区
ZonedDateTime specificDateTime = ZonedDateTime.of(2025, 5, 15, 14, 30, 0, 0, ZoneId.of("Asia/Shanghai"));
System.out.println("指定时间和时区: " + specificDateTime);
}
}
常用方法
now()
:获取当前时间和时区。of(year, month, day, hour, minute, second, nanoOfSecond, zone)
:创建指定时区的日期和时间。plusHours(long hours)
:增加小时数。withZoneSameInstant(ZoneId zone)
:调整为指定时区的时间。getZone()
:获取时区信息。
5. Instant:表示时间戳(自1970年1月1日起的秒数)
Instant
表示时间点,通常用于时间戳。它是自 UNIX 纪元(1970年1月1日)以来的秒数。适用于精确时间和存储时间。
创建 Instant 实例
import java.time.Instant;
public class InstantExample {
public static void main(String[] args) {
// 当前时间戳
Instant now = Instant.now();
System.out.println("当前时间戳: " + now);
// 从秒数创建
Instant specificInstant = Instant.ofEpochSecond(1672531199);
System.out.println("指定时间戳: " + specificInstant);
}
}
常用方法
now()
:获取当前的时间戳。ofEpochSecond(long epochSecond)
:通过给定的秒数创建Instant
。plusSeconds(long seconds)
:增加秒数。minusMillis(long millis)
:减少毫秒数。toEpochMilli()
:返回自 Unix 纪元以来的毫秒数。
6. Duration 和 Period
- Duration 用于表示时间上的差异,通常用于 秒 和 纳秒 级别的操作。
- Period 用于表示日期上的差异,通常用于 年、月 和 日。
Duration 示例
import java.time.Duration;
import java.time.LocalTime;
public class DurationExample {
public static void main(String[] args) {
LocalTime startTime = LocalTime.of(10, 30);
LocalTime endTime = LocalTime.of(12, 45);
Duration duration = Duration.between(startTime, endTime);
System.out.println("持续时间(小时和分钟): " + duration.toHours() + "小时 " + duration.toMinutes() % 60 + "分钟");
}
}
Period 示例
import java.time.LocalDate;
import java.time.Period;
public class PeriodExample {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2020, 1, 1);
LocalDate endDate = LocalDate.of(2025, 5, 15);
Period period = Period.between(startDate, endDate);
System.out.println("日期差异: " + period.getYears() + "年 " + period.getMonths() + "月 " + period.getDays() + "天");
}
}
常用方法
Duration.between(start, end)
:计算两个时间点的持续时间。Period.between(start, end)
:计算两个日期之间的差异。
总结
- LocalDate:用于处理没有时间部分的日期。
- LocalTime:用于处理没有日期部分的时间。
- LocalDateTime:用于处理日期和时间,但不带时区。
- ZonedDateTime:用于处理日期和时间,并带有时区。
- Instant:表示时间戳,精确到秒和纳秒。
- Duration 和 Period:分别表示时间和日期的差异。
这些类使得日期和时间的处理更加直观、安全,并且支持更复杂的日期时间计算。