文章目录
一、引入
在刚刚我们已经学习完了新增时间类的前两部分。
第一部分,它跟JDK7以前的 Date类
是类似的,它们的对象就表示当前时间的对象 / 指定时间的对象。
第二部分 DateTimeFormatter
,它跟JDK7以前的 SimpleDateFormat
是类似的,可以用于时间日期的格式化。
再往下,我们接下来学习第三类。在JDK7以前的时间类中,还有一个 Calendar
日历类,通过这个日历类,就可以对时间里面单独的年、月、日进行单独的操作。
JDK8同样也有类似的跟日历相关的类,一共有三个。
LocalDate
,它里面包含了年、月、日
,这个类是没有时、分、秒
的LocalTime
:它里面包含了时、分、秒
,这个类是没有年、月、日
的LocalDateTime
:它就包含了所有年、月、日、时、分、秒
在这三个类中,它里面的方法也是非常有特点的,跟之前一样。
![image-20240424155651825](https://img-blog.csdnimg.cn/img_convert/75d9d5f3fd5c1c4780a120857e3d1758.png)
除此之外,LocalDateTime
还可以转成 LocalDate
、LocalTime
,因为它是最长的,用的方法就是 to
开头的方法。
二、LocalDate
它只包含 年、月、日
,没有 时、分、秒
1)获取当前时间的日历对象
如果我们调用静态方法 now()
,就可以获取到当前时间的日历对象。
LocalDate nowDate = LocalDate.now();
System.out.println("今天的日期:" + nowDate); // 今天的日期:2024-04-24
2)获取指定时间的日历对象
of()
方法中可以传递 年、月、日
,将指定的时间写进去。
![image-20240424160424257](https://img-blog.csdnimg.cn/img_convert/207d1bb3ba0a9b4319d330f22da2dfd9.png)
LocalDate ldDate = LocalDate.of(2023, 1, 1);
System.out.println("指定日期:" + ldDate);
3)获取到日历中的每一个属性值
getXXX
的方法可以获取到日历中的每一个属性值
int year = ldDate.getYear();
System.out.println("year: " + year);
要注意,在获取月的时候,获取到的是月的对象
Month m = ldDate.getMonth();
如果直接打印的话,打印到的是对象里面的信息
System.out.println(m); // JANUARY
但如果我不行看到这个,我只想看到阿拉伯数字,有两种办法。
方法一:用月的对象再去调用 getValue()
方法
System.out.println(m.getValue()); // 1
方式二:我们在用 LocalDate对象
获取数据的时候换一个方法:getMonthValue()
,即获取月份的数据信息。
此时方法会返回一个int值
,这个值就是阿拉伯数字
int month = ldDate.getMonthValue();
System.out.println("month: " + month); // month: 1
获取日期,getDayOfMonth
:月中的某一天,即几号
int day = ldDate.getDayOfMonth();
System.out.println("day:" + day);
获取一年中的第几天
int dayofYear = ldDate.getDayOfYear();
System.out.println("dayOfYear:" + dayofYear);
获取星期,只不过这个方法返回的也是星期的对象
DayOfWeek dayOfWeek = ldDate.getDayOfWeek();
System.out.println(dayOfWeek);
如果想要看到阿拉伯数字,还需要再调用 getValue()
方法就行了,跟上面的 Month
是类似的
System.out.println(dayOfWeek.getValue());
PS:上面的方法不需要去背,你只要知道 get
开头的就是获取就行了,我们直接使用 对象
调用 get
,IDEA都会有自动提示,然后根据提示出来的方法名自己猜也能猜到。
![image-20240424161922558](https://img-blog.csdnimg.cn/img_convert/16e43fe98d186a3a4bb08fec192eaba9.png)
4)判断系列的方法
is
开头的方法表示判断,判断两个时间谁在前谁在后。
System.out.println(ldDate.isBefore(ldDate));
System.out.println(ldDate.isAfter(ldDate));
5)修改时间系列的方法
with开头的方法表示修改。PS:只能修改年月日。
下面例子就是将年修改为 2000
。
LocalDate withLocalDate = ldDate.withYear(2000);
System.out.println(withLocalDate);
验证:跟之前一样,对象里面的数据是不能发生改变的,如果你要修改,它会产生一个新的 LocalDate对象
。
我们可以拿原来的日期对象跟修改之后的日期对象做一个 ==号
的比较
System.out.println(withLocalDate == ldDate); // false,false就代表它们两个不是同一个对象
6)减少时间系列的方法
minus开头的方法表示减少,只能减少年月日
![image-20240424162723170](https://img-blog.csdnimg.cn/img_convert/278b1db06355e4d9154e936b1fc8c644.png)
PS:如果参数传入的是负数,那就是往前加,但是这种方式比较反人类
LocalDate minusLocalDate = ldDate.minusYears(1);
System.out.println(minusLocalDate);
7)增加时间系列的方法
plus开头的方法表示增加,只能增加年月日
PS:如果参数传入的是负数,那就是往前减,但是这种方式比较反人类
LocalDate plusLocalDate = ldDate.plusDays(1);
System.out.println(plusLocalDate);
8)扩展
利用这个类就可以解释我们平时在玩手机的时候遇到的一个场景:每年你过生日的时候那些银行、移动、联通都会给你发一些短信,它是怎么实现的呢?
其实就可以用我们刚刚所学习的 LocalDate
表示。
判断你今天是不是过生日,跟后面的 时、分、秒
没关系,我只要 月、日
就行了。
因此在这就可以写两个对象。
第一个:用 LocalDate
调用 of()
,把你的出生年月日封装成 LocalDate对象
第二个:将当前时间封装成 LocalDate对象
。
LocalDate birDate = LocalDate.of(2000, 1, 1);
LocalDate nowDate1 = LocalDate.now();
还没完,这里又出现了一个新的类:MonthDay
,它表示的是 月、日
对象。
我们可以选中 MonthDay
ctrl + b 看下源码
在 MonthDay
的 of()
中可以指定月份、日期,将它们两个封装成 月、日
对象。
然后再把今天的时间去封装成 月、日
对象
MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth());
MonthDay nowMd = MonthDay.from(nowDate1);
最后将两个 月、日
对象进行比较,如果相同,就表示你过生日;如果为 false
,就表示你今天不过生日,明天再判断就行了。
System.out.println("今天是你的生日吗? " + birMd.equals(nowMd)); // 今天是你的生日吗?
三、LocalTime
它的用法跟 LocalDate
一样,只不过它里面包含的是 时、分、秒
1)代码示例
// 获取本地时间的日历对象。(包含 时分秒)
LocalTime nowTime = LocalTime.now();
System.out.println("今天的时间:" + nowTime);
int hour = nowTime.getHour();//时
System.out.println("hour: " + hour);
int minute = nowTime.getMinute();//分
System.out.println("minute: " + minute);
int second = nowTime.getSecond();//秒
System.out.println("second:" + second);
int nano = nowTime.getNano();//纳秒
System.out.println("nano:" + nano);
System.out.println("------------------------------------");
System.out.println(LocalTime.of(8, 20));//时分
System.out.println(LocalTime.of(8, 20, 30));//时分秒
System.out.println(LocalTime.of(8, 20, 30, 150));//时分秒纳秒
LocalTime mTime = LocalTime.of(8, 20, 30, 150);
//is系列的方法
System.out.println(nowTime.isBefore(mTime));
System.out.println(nowTime.isAfter(mTime));
//with系列的方法,只能修改时、分、秒
System.out.println(nowTime.withHour(10));
//plus系列的方法,只能修改时、分、秒
System.out.println(nowTime.plusHours(10));
2)应用场景
这个类只关注 时、分、秒、纳秒
,所以在计算时间的时候,它比较精确。
例如之前说过的秒杀活动。在秒杀活动中,差一秒钟都会导致你这个活动没有参加上,因此在这种情况下,对时间的精确性非常的有要求,并且此时 年月日
在这里也是没有用的,这种情况下关注更多的是 时分秒
,因此此时会使用 LocalTime
更多一些。
四、LocalDateTime
这个类中包含的东西是最全的: 年、月、日、时、分、秒
,并且用法跟之前一样。
这些代码不用自己写,将下面示例代码导入IDEA中看看它们的作用就行了。
// 当前时间的的日历对象(包含年月日时分秒)
LocalDateTime nowDateTime = LocalDateTime.now();
System.out.println("今天是:" + nowDateTime);//今天是:
System.out.println(nowDateTime.getYear());//年
System.out.println(nowDateTime.getMonthValue());//月
System.out.println(nowDateTime.getDayOfMonth());//日
System.out.println(nowDateTime.getHour());//时
System.out.println(nowDateTime.getMinute());//分
System.out.println(nowDateTime.getSecond());//秒
System.out.println(nowDateTime.getNano());//纳秒
// 日:当年的第几天
System.out.println("dayofYear:" + nowDateTime.getDayOfYear());
//星期
System.out.println(nowDateTime.getDayOfWeek());
System.out.println(nowDateTime.getDayOfWeek().getValue());
//月份
System.out.println(nowDateTime.getMonth());
System.out.println(nowDateTime.getMonth().getValue());
LocalDate ld = nowDateTime.toLocalDate();
System.out.println(ld);
LocalTime lt = nowDateTime.toLocalTime();
System.out.println(lt.getHour());
System.out.println(lt.getMinute());
System.out.println(lt.getSecond());