Java LocalDate日期时间类的用法(附带实例)
JDK 1.8 中,新增了 LocalDate 类、LocalTime 类、LocalDateTime 类等几个日期时间类,位于 java.time 包中,都是不可变的。在日期时间的处理上,这些类提供了更丰富的方法以及更清晰的格式,逐渐替代了之前的 Date 类。本节带大家了解 LocalDate 类的用法。
LocalDate 类用来表示日期,只有年月日,没有时分秒。
LocalDate 类不能通过 new 关键字来创建对象,它提供了两种获取对象的方法:
根据指定年月日来创建日期对象。具体语法如下:
除 now() 和 of() 方法外,LocalDate 类还提供了获取日期信息、日期运算、格式化的方法,常用方法如下表所示:
接下来,通过案例来演示 LocalDate 类常用方法的使用。
LocalDate 类用来表示日期,只有年月日,没有时分秒。
LocalDate 类不能通过 new 关键字来创建对象,它提供了两种获取对象的方法:
- 一个是使用 now() 方法获取系统当前时间;
- 另外一个是使用of(int year, int month, int dayOfMonth)
根据指定年月日来创建日期对象。具体语法如下:
LocalDate 变量名 = LocalDate.now(); LocalDate 变量名 = LocalDate.of(年,月,日);
除 now() 和 of() 方法外,LocalDate 类还提供了获取日期信息、日期运算、格式化的方法,常用方法如下表所示:
方法 | 方法描述 |
---|---|
LocalDate now() | 获取当前日期 |
LocalDate of(int year, int month, int dayOfMonth) | 根据参数设置日期,参数分别为年、月、日 |
int getDayOfMonth() | 获取当前日期是所在月的第几天 |
DayOfWeek getDayOfWeek() | 获取当前日期所在星期,是一个 DayOfWeek 枚举类的值 |
int getDayOfYear() | 获取当前日期是所在年的第几天 |
Month getMonth() | 获取当前日期所在月份,是一个 Month 枚举类的值 |
int getMonthValue() | 获取当前日期所在月份的数值 |
int lengthOfMonth() | 获取当前日期所在月份有多少天 |
int lengthOfYear() | 获取当前日期所在年有多少天 |
boolean isEqual(LocalDate other) | 判断两个日期是否相同 |
boolean isLeapYear() | 获取当前日期所在年是否为闰年 |
LocalDate withDayOfMonth(int dayOfMonth) | 指定当前月的第几天,返回一个新的日期,不影响当前 |
LocalDate withDayOfYear(int dayOfYear) | 指定当前年的第几天,返回一个新的日期,不影响当前 |
LocalDate withMonth(int month) | 指定月,返回一个新的日期,不影响当前 |
LocalDate withYear(int year) | 指定年,返回一个新的日期,不影响当前 |
LocalDate minusDays(long days) | 将当前日期减 days 天,返回一个新的日期,不影响当前 |
LocalDate minusWeeks(long weeks) | 将当前日期减 weeks 周,返回一个新的日期,不影响当前 |
LocalDate minusMonths(long months) | 将当前日期减 months 月,返回一个新的日期,不影响当前 |
LocalDate minusYears(long years) | 将当前日期减 years 年,返回一个新的日期,不影响当前 |
LocalDate plusDays(long days) | 将当前日期加 days 天,返回一个新的日期,不影响当前 |
LocalDate plusWeeks(long weeks) | 将当前日期加 weeks 周,返回一个新的日期,不影响当前 |
LocalDate plusMonths(long months) | 将当前日期加 months 月,返回一个新的日期,不影响当前 |
LocalDate plusYears(long years) | 将当前日期加 years 年,返回一个新的日期,不影响当前 |
接下来,通过案例来演示 LocalDate 类常用方法的使用。
import java.time.LocalDate; public class Demo { public static void main(String[] args) { LocalDate date = LocalDate.now(); System.out.println("系统当前日期:" + date); LocalDate ofDate = LocalDate.of(2021, 12, 12); System.out.println("of获取的日期信息:" + ofDate); // 该方法会返回一个新的日期对象,对原来的日期对象并不会更改 ofDate = ofDate.withDayOfMonth(30); // 参数是这个月的第30天 System.out.println("withDayOfMonth(30):" + ofDate); ofDate = ofDate.withDayOfYear(30); // 参数是这一年的第30天 System.out.println("withDayOfYear(30):" + ofDate); ofDate = ofDate.plusDays(30); // 加30天,返回一个新的日期 System.out.println("plusDays(30):" + ofDate); System.out.println("getMonthValue:" + ofDate.getMonthValue()); System.out.println("lengthOfMonth:" + ofDate.lengthOfMonth()); System.out.println("lengthOfYear:" + ofDate.lengthOfYear()); System.out.println("两个日期是否相等:" + ofDate.isEqual(date)); } }程序的运行结果如下:
系统当前日期:2025-04-16 of获取的日期信息:2021-12-12 withDayOfMonth(30):2021-12-30 withDayOfYear(30):2021-01-30 plusDays(30):2021-03-01 getMonthValue:3 lengthOfMonth:31 lengthOfYear:365 两个日期是否相等:false程序中,每次调用日期操作方法时都需要重新赋值,因为 LocalDate 对象不可变,方法返回的都是一个新的对象。