介绍
LocalDate、LocalTime和LocalDateTime是Java 8中引入的日期和时间类,它们都属于java.time包。
-
LocalDate:表示日期,不包含时间和时区信息。它可以用于存储和操作年、月、日的值。例如,可以使用LocalDate来表示生日、会议日期等。
-
LocalTime:表示时间,不包含日期和时区信息。它可以用于存储和操作时、分、秒的值。例如,可以使用LocalTime来表示每天的固定时间点,如午夜、中午等。
-
LocalDateTime:表示日期和时间,不包含时区信息。它是LocalDate和LocalTime的组合。可以使用LocalDateTime来表示具体的日期和时间,例如某个事件发生的时间。
这些类都是不可变的,任何对它们的修改操作都会返回一个新的实例。它们提供了一系列方法用于获取、设置和操作日期和时间的不同部分,以及进行比较、格式化、解析等操作。
需要注意的是,这些类不考虑时区的影响。如果需要处理时区相关的日期和时间,可以使用ZonedDateTime类。
示例
LocalDate:
- 表示日期,不包含时间和时区信息。它可以用于存储和操作年、月、日的值。
// 获取当前日期
LocalDate currentDate = LocalDate.now();
System.out.println("当前日期: " + currentDate);
// 创建指定日期
LocalDate specificDate = LocalDate.of(2023, 7, 19);
System.out.println("指定日期: " + specificDate);
// 字符串转日期
String dateString = "2023-07-19";
LocalDate parsedDate = LocalDate.parse(dateString);
System.out.println("解析后的日期: " + parsedDate);
// 日期转字符串
String formattedDate = specificDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println("格式化后的日期: " + formattedDate);
// 日期加减
LocalDate tomorrow = currentDate.plusDays(1);
LocalDate lastWeek = currentDate.minusWeeks(1);
System.out.println("明天的日期: " + tomorrow);
System.out.println("一周前的日期: " + lastWeek);
// 比较日期
boolean isAfter = specificDate.isAfter(currentDate);
boolean isBefore = specificDate.isBefore(currentDate);
boolean isEqual = specificDate.isEqual(currentDate);
System.out.println("指定日期是否在当前日期之后: " + isAfter);
System.out.println("指定日期是否在当前日期之前: " + isBefore);
System.out.println("指定日期是否等于当前日期: " + isEqual);
LocalTime:
- LocalTime(时间):表示时间,不包含日期和时区信息。它可以用于存储和操作时、分、秒的值。
// 获取当前时间
LocalTime currentTime = LocalTime.now();
System.out.println("当前时间: " + currentTime);
// 创建指定时间
LocalTime specificTime = LocalTime.of(12, 30, 45);
System.out.println("指定时间: " + specificTime);
// 字符串转时间
String timeString = "12:30:45";
LocalTime parsedTime = LocalTime.parse(timeString);
System.out.println("解析后的时间: " + parsedTime);
// 时间转字符串
String formattedTime = specificTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
System.out.println("格式化后的时间: " + formattedTime);
// 时间加减
LocalTime nextHour = currentTime.plusHours(1);
LocalTime previousMinute = currentTime.minusMinutes(1);
System.out.println("下一个小时的时间: " + nextHour);
System.out.println("上一分钟的时间: " + previousMinute);
// 比较时间
boolean isAfter = specificTime.isAfter(currentTime);
boolean isBefore = specificTime.isBefore(currentTime);
System.out.println("指定时间是否在当前时间之后: " + isAfter);
System.out.println("指定时间是否在当前时间之前: " + isBefore);
LocalDateTime:
- LocalDateTime(日期时间):表示日期和时间,不包含时区信息。它是LocalDate和LocalTime的组合。
// 获取当前日期时间
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("当前日期时间: " + currentDateTime);
// 创建指定日期时间
LocalDateTime specificDateTime = LocalDateTime.of(2023, 7, 19, 12, 30, 45);
System.out.println("指定日期时间: " + specificDateTime);
// 字符串转日期时间
String dateTimeString = "2023-07-19T12:30:45";
LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeString);
System.out.println("解析后的日期时间: " + parsedDateTime);
// 日期时间转字符串
String formattedDateTime = specificDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("格式化后的日期时间: " + formattedDateTime);
// 日期时间加减
LocalDateTime nextWeek = currentDateTime.plusWeeks(1);
LocalDateTime previousMonth = currentDateTime.minusMonths(1);
System.out.println("下一周的日期时间: " + nextWeek);
System.out.println("上个月的日期时间: " + previousMonth);
// 比较日期时间
boolean isAfter = specificDateTime.isAfter(currentDateTime);
boolean isBefore = specificDateTime.isBefore(currentDateTime);
boolean isEqual = specificDateTime.isEqual(currentDateTime);
System.out.println("指定日期时间是否在当前日期时间之后: " + isAfter);
System.out.println("指定日期时间是否在当前日期时间之前: " + isBefore);
System.out.println("指定日期时间是否等于当前日期时间: " + isEqual);