首页 > 编程笔记 > Java笔记 阅读:38

Java LocalDate、LocalTime和LocalDateTime日期时间类的用法(附带实例)

JDK 1.8 中,新增了 LocalDate 类、LocalTime 类、LocalDateTime 类等几个日期时间类,位于 java.time 包中,都是不可变的。在日期时间的处理上,这些类提供了更丰富的方法以及更清晰的格式,逐渐替代了之前的 Date 类。

Java LocalDate类

LocalDate 类用来表示日期,只有年月日,没有时分秒。

LocalDate 类不能通过 new 关键字来创建对象,它提供了两种获取对象的方法:
根据指定年月日来创建日期对象。具体语法如下:
LocalDate 变量名 = LocalDate.now();
LocalDate 变量名 = LocalDate.of(年,月,日);

除 now() 和 of() 方法外,LocalDate 类还提供了获取日期信息、日期运算、格式化的方法,常用方法如下表所示:

表: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 对象不可变,方法返回的都是一个新的对象。

Java LocalTime类

LocalTime 类产生的对象是一个不可变的时间对象,以纳秒精度表示,此类只存储时分秒,而不存储日期和时区。

LocalTime 类也不能通过 new 关键字来创建对象,它同样提供了两个获取对象的方法:
具体语法如下:
LocalTime 变量名 = LocalTime.now();
LocalTime 变量名 = LocalTime.of(时,分);
LocalTime 变量名 = LocalTime.of(时,分,秒);
LocalTime 变量名 = LocalTime.of(时,分,秒,纳秒);

除 now() 和 of() 方法外,LocalTime 类还提供了获取时间信息、进行时间运算、格式化的方法,常用方法与 LocalDate 类相类似,此处不在一一说明。

接下来,通过案例来演示 LocalTime 类常用方法的使用:
import java.time.LocalTime;

public class Demo {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        System.out.println("系统当前时间:" + time);
        LocalTime ofTime = LocalTime.of(12,12,12);
        System.out.println("of获取的时间信息:" + ofTime);
        ofTime = ofTime.withHour(20);  // 设置小时为20
        System.out.println("ofTime.withHour(20):" + ofTime);
        ofTime = ofTime.plusMinutes(60);  // 加60分钟
        System.out.println("ofTime.plusMinutes(60):" + ofTime);
        int hours = ofTime.getHour();  // 获取小时
        System.out.println("ofTime.getHour():" + hours);
    }
}
程序的运行结果如下:
系统当前时间:02:26:49.524986
of获取的时间信息:12:12:12
ofTime.withHour(20):20:12:12
ofTime.plusMinutes(60):21:12:12
ofTime.getHour():21

Java LocalDateTime类

LocalDateTime 类可以设置年月日时分秒,是 LocalDate 类和 LocalTIme 类的综合,可以通过 now() 和 of() 方法获取对象。

LocalDateTime 类同时含有LocalDate类和LocalTime类的方法,而且还提供了额外的转换方法。

接下来,通过案例来演示 LocalDateTime 对象的获取以及常用方法的使用。
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class Demo {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();  // 通过now()获取系统当前时间
        System.out.println("系统当前时间:" + dateTime);
        LocalDateTime ofD = LocalDateTime.of(2021,12,12,10,10,10);  // 通过of获取对象
        System.out.println("通过of(年,月,日,时,分,秒)创建的时间:" + ofD);
        LocalDate date = LocalDate.of(2021,12,12);
        LocalTime time = LocalTime.of(10,10,10);
        // 通过LocalDate和LocalTime组合成LocalDateTime对象
        LocalDateTime ofD1 = LocalDateTime.of(date,time);
        System.out.println("LocalDate与LocalTime组合时间:" + ofD1);
        ofD = ofD.withDayOfMonth(2);  // 指定月
        System.out.println("ofD.withDayOfMonth(2):" + ofD);
        ofD = ofD.plusMonths(1);  // 加1个月
        System.out.println("ofD.plusMonths(1):" + ofD);
        LocalDate localDate = ofD.toLocalDate();  // 获取LocalDate
        System.out.println("ofD.toLocalDate():" + localDate);
        // LocalDate加上LocalTime组成LocalDateTime
        LocalDateTime dateTime1 = localDate.atTime(LocalTime.now());
        System.out.println("localDate.atTime(LocalTime.now()):" + dateTime1);
    }
}
程序的运行结果如下:
系统当前时间:2025-04-16T02:18:27.882659
通过of(年,月,日,时,分,秒)创建的时间:2021-12-12T10:10:10
LocalDate与LocalTime组合时间:2021-12-12T10:10:10
ofD.withDayOfMonth(2):2021-12-02T10:10:10
ofD.plusMonths(1):2022-01-02T10:10:10
ofD.toLocalDate():2022-01-02
localDate.atTime(LocalTime.now()):2022-01-02T02:18:27.891053
程序中演示了 LocalDateTime 对象的多种获取形式和常用方法,基本操作与 LocalDate 对象和 LocalTime 对象一样。

除此之外,还演示了几个类之间的转换:

相关文章