一、其他日期处理方式
在 Java 中处理日期和时间,你可以使用 java.util.Date
类和 java.util.Calendar
类,或者从 Java 8 开始引入的 java.time
包(JSR-310),后者提供了更加强大和灵活的日期时间API。
分别介绍如何在 JDK 8 中使用
java.util.Date
和java.util.Calendar
以及如何用java.time
包来处理日期。
1、 使用 java.util.Date
private static void my1() {
// 获取当前日期和时间
Date now = new Date();
System.out.println("当前日期和时间: " + now);
// 转换为特定格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = sdf.format(now);
System.out.println("格式化后的日期和时间: " + formattedDate);
}
2、使用 java.util.Calendar
private static void my2() {
// 获取 Calendar 实例
Calendar calendar = Calendar.getInstance();
// 设置日期和时间
calendar.set(2023, Calendar.MARCH, 15, 10, 30, 0); // 注意月份是从0开始的,所以3月是2,年份是2023
// 获取并打印日期和时间
System.out.println("设置后的日期和时间: " + calendar.getTime());
}
3、使用 java.time 包 (推荐)
从 Java 8 开始,java.time
包提供了更好的日期时间API。
1、使用 LocalDate 和 LocalTime
private static void my3() {
// 获取当前日期和时间
LocalDateTime now = LocalDateTime.now();
System.out.println("当前日期和时间: " + now);
// 格式化输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedNow = now.format(formatter);
System.out.println("格式化后的日期和时间: " + formattedNow);
}
2、使用 ZonedDateTime 处理时区
private static void my4() {
// 获取当前时区的时间
ZonedDateTime now = ZonedDateTime.now(); // 使用系统默认时区
System.out.println("当前时区的时间: " + now);
// 指定时区并获取时间
ZonedDateTime specificZoneTime = ZonedDateTime.now(ZoneId.of("America/New_York")); // 使用纽约时区作为例子
System.out.println("纽约时区的时间: " + specificZoneTime);
// 格式化输出
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); // 包含时区信息
String formattedTime = specificZoneTime.format(formatter);
System.out.println("格式化后的纽约时区时间: " + formattedTime);
}
对于简单的日期时间操作,可以使用
java.util
中的Date
和Calendar
。但这些类设计得较为复杂,且不是线程安全的。对于更复杂、更现代的日期时间处理需求,建议使用 Java 8 引入的
java.time
包中的类,如LocalDate
、LocalTime
、LocalDateTime
、ZonedDateTime
等。这些类提供了更好的设计,并且是不可变的,线程安全。
二、JDK8 日期常用类和方法
三、日期的使用
// 创建 LocalDate, LocalTime, LocalDateTime 对象
LocalDate today = LocalDate.now();
LocalTime now = LocalTime.now();
LocalDateTime nowDateTime = LocalDateTime.now();
// 打印当前日期和时间
System.out.println("当前日期: " + today);
System.out.println("当前时间: " + now);
System.out.println("当前日期和时间: " + nowDateTime);
// 自定义日期和时间
LocalDate customDate = LocalDate.of(2023, 10, 1);
LocalTime customTime = LocalTime.of(14, 30, 45);
LocalDateTime customDateTime = LocalDateTime.of(2023, 10, 1, 14, 30, 45);
// 打印自定义日期和时间
System.out.println("自定义日期: " + customDate);
System.out.println("自定义时间: " + customTime);
System.out.println("自定义日期和时间: " + customDateTime);
// 使用 DateTimeFormatter 格式化日期和时间
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = today.format(dateFormatter);
String formattedTime = now.format(timeFormatter);
String formattedDateTime = nowDateTime.format(dateTimeFormatter);
System.out.println("格式化后的当前日期: " + formattedDate);
System.out.println("格式化后的当前时间: " + formattedTime);
System.out.println("格式化后的当前日期和时间: " + formattedDateTime);
// 计算两个日期之间的间隔(Period)
LocalDate startDate = LocalDate.of(2020, 1, 1);
LocalDate endDate = LocalDate.of(2023, 10, 1);
Period period = Period.between(startDate, endDate);
System.out.println("两个日期之间的间隔: " + period.getYears() + " 年, " +
period.getMonths() + " 月, " + period.getDays() + " 天");
// 计算两个时间点之间的间隔(Duration)
LocalDateTime startTime = LocalDateTime.of(2023, 10, 1, 10, 0, 0);
LocalDateTime endTime = LocalDateTime.of(2023, 10, 1, 14, 30, 45);
Duration duration = Duration.between(startTime, endTime);
System.out.println("两个时间点之间的间隔: " + duration.toHours() + " 小时, " +
DurationUtils.toMinutesPart(duration) + " 分钟, " + DurationUtils.toSecondsPart(duration) + " 秒");
// 注意:Duration 没有直接获取分钟和秒部分的方法,需要手动计算
long hours = duration.toHours();
long minutes = duration.toMinutes() % 60;
long seconds = duration.getSeconds() % 60;
System.out.println("两个时间点之间的间隔(详细): " + hours + " 小时, " +
minutes + " 分钟, " + seconds + " 秒");
}
}
// 扩展 Duration 类以获取分钟和秒的剩余部分(Java 标准库中没有直接的方法)
class DurationUtils {
public static long toMinutesPart(Duration duration) {
return duration.toMinutes() % 60;
}
public static long toSecondsPart(Duration duration) {
return duration.getSeconds() % 60;
}
}