📚 Java 时间处理函数详解
🔖 1. 概述
Java 中有多种 时间处理类 和 函数 来处理日期和时间。这些类在不同的版本中不断演化,从早期的 Date 和 Calendar,到 Java 8 引入的 java.time API,这些新 API 提供了更强大的时间处理能力,包括 日期、时间、时区、格式化、解析、时间间隔 等功能。
🔗 2. Java 中常用的时间处理类
| 类/接口 | 包 | 描述 |
|---|---|---|
LocalDate | java.time | 表示日期,不包含时间 |
LocalTime | java.time | 表示时间,不包含日期 |
LocalDateTime | java.time | 表示日期和时间,不包含时区信息 |
ZonedDateTime | java.time | 包含时区的日期时间 |
Instant | java.time | 表示一个时间戳 |
Duration | java.time | 表示两个时间之间的间隔 |
Period | java.time | 表示两个日期之间的间隔 |
DateTimeFormatter | java.time.format | 日期时间格式化和解析 |
ZoneId | java.time | 表示时区 |
📋 3. LocalDate 和 LocalTime 的使用
✅ 3.1 LocalDate 的使用
LocalDate 表示 日期(例如:2025-01-06),不包含时间和时区信息。
🔧 使用案例
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
System.out.println("Current Date: " + currentDate);
// 创建一个指定日期
LocalDate specificDate = LocalDate.of(2025, 1, 6);
System.out.println("Specific Date: " + specificDate);
// 获取日期的年、月、日
int year = currentDate.getYear();
int month = currentDate.getMonthValue();
int day = currentDate.getDayOfMonth();
System.out.println("Year: " + year + ", Month: " + month + ", Day: " + day);
// 判断是否是闰年
boolean isLeapYear = currentDate.isLeapYear();
System.out.println("Is Leap Year: " + isLeapYear);
}
}
输出:
Current Date: 2025-01-06
Specific Date: 2025-01-06
Year: 2025, Month: 1, Day: 6
Is Leap Year: false
✅ 3.2 LocalTime 的使用
LocalTime 表示 时间(例如:10:15:30),不包含日期和时区信息。
🔧 使用案例
import java.time.LocalTime;
public class LocalTimeExample {
public static void main(String[] args) {
// 获取当前时间
LocalTime currentTime = LocalTime.now();
System.out.println("Current Time: " + currentTime);
// 创建一个指定时间
LocalTime specificTime = LocalTime.of(10, 15, 30);
System.out.println("Specific Time: " + specificTime);
// 获取时间的小时、分钟、秒
int hour = currentTime.getHour();
int minute = currentTime.getMinute();
int second = currentTime.getSecond();
System.out.println("Hour: " + hour + ", Minute: " + minute + ", Second: " + second);
}
}
输出:
Current Time: 10:20:45.123
Specific Time: 10:15:30
Hour: 10, Minute: 20, Second: 45
📆 4. LocalDateTime 的使用
LocalDateTime 表示 日期和时间,不包含时区信息。
🔧 使用案例
import java.time.LocalDateTime;
public class LocalDateTimeExample {
public static void main(String[] args) {
// 获取当前日期时间
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current DateTime: " + currentDateTime);
// 创建一个指定日期时间
LocalDateTime specificDateTime = LocalDateTime.of(2025, 1, 6, 10, 15, 30);
System.out.println("Specific DateTime: " + specificDateTime);
// 加减日期时间
LocalDateTime nextDay = currentDateTime.plusDays(1);
System.out.println("Next Day: " + nextDay);
LocalDateTime previousHour = currentDateTime.minusHours(1);
System.out.println("Previous Hour: " + previousHour);
}
}
输出:
Current DateTime: 2025-01-06T10:20:45.123
Specific DateTime: 2025-01-06T10:15:30
Next Day: 2025-01-07T10:20:45.123
Previous Hour: 2025-01-06T09:20:45.123
🕒 5. ZonedDateTime 的使用
ZonedDateTime 表示 包含时区的日期时间。
🔧 使用案例
import java.time.ZonedDateTime;
import java.time.ZoneId;
public class ZonedDateTimeExample {
public static void main(String[] args) {
// 获取当前时区的日期时间
ZonedDateTime currentZonedDateTime = ZonedDateTime.now();
System.out.println("Current ZonedDateTime: " + currentZonedDateTime);
// 创建指定时区的日期时间
ZonedDateTime specificZonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println("Specific ZonedDateTime: " + specificZonedDateTime);
}
}
输出:
Current ZonedDateTime: 2025-01-06T10:20:45.123+01:00[Europe/Paris]
Specific ZonedDateTime: 2025-01-06T04:20:45.123-05:00[America/New_York]
🕰️ 6. Instant 的使用
Instant 表示一个 时间戳,通常用于记录事件的发生时间。
🔧 使用案例
import java.time.Instant;
public class InstantExample {
public static void main(String[] args) {
// 获取当前时间戳
Instant currentInstant = Instant.now();
System.out.println("Current Instant: " + currentInstant);
// 将 Instant 转换为毫秒
long epochMilli = currentInstant.toEpochMilli();
System.out.println("Epoch Milliseconds: " + epochMilli);
}
}
🧮 7. 时间间隔的计算
✅ Duration(时间间隔)
import java.time.Duration;
import java.time.LocalTime;
public class DurationExample {
public static void main(String[] args) {
LocalTime startTime = LocalTime.of(10, 0);
LocalTime endTime = LocalTime.of(12, 30);
Duration duration = Duration.between(startTime, endTime);
System.out.println("Duration: " + duration.toHours() + " hours");
}
}
✅ 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, 1, 1);
Period period = Period.between(startDate, endDate);
System.out.println("Period: " + period.getYears() + " years");
}
}
📅 8. 日期时间格式化
✅ 使用 DateTimeFormatter
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterExample {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();
// 自定义格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = dateTime.format(formatter);
System.out.println("Formatted DateTime: " + formattedDateTime);
}
}
🎯 9. 总结
| 类/接口 | 描述 |
|---|---|
LocalDate | 表示日期 |
LocalTime | 表示时间 |
LocalDateTime | 表示日期和时间 |
ZonedDateTime | 表示带时区的日期时间 |
Instant | 表示时间戳 |
Duration | 表示两个时间点之间的时间间隔 |
Period | 表示两个日期之间的日期间隔 |
DateTimeFormatter | 日期时间格式化 |
2384

被折叠的 条评论
为什么被折叠?



