2025年java中常见时间操作

java中常见时间操作1 获取当前日期 只能精确到年月日 String formatStr yyyy MM dd LocalDate now LocalDate now DateTimeForm dateTimeForm DateTimeForm ofPattern formatStr

大家好,我是讯享网,很高兴认识大家。


1.获取当前日期(只能精确到年月日)

 String formatStr = "yyyy-MM-dd"; LocalDate now = LocalDate.now(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatStr); String nowFormat = now.format(dateTimeFormatter); System.out.println("格式化后的当前日期:"+nowFormat);

讯享网

结果: 格式化后的当前日期:2022-03-28

2.获取当前时间(可以精确到秒)

讯享网 String formatStr = "yyyy-MM-dd HH:mm:ss"; LocalDateTime now = LocalDateTime.now(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatStr); String nowFormat = now.format(dateTimeFormatter); System.out.println("格式化后的当前日期:"+nowFormat);

结果: 格式化后的当前日期:2022-03-28 10:15:51


讯享网

 

3.时间大小比较

 LocalDate now = LocalDate.now(); LocalDate date1 = LocalDate.of(2022, 05, 01); LocalDate date2 = LocalDate.of(2022, 04, 27); LocalDate date3 = LocalDate.of(2022, 04, 28); System.out.printf("是否是同一时间:%s ", now.equals(date1)); System.out.println(); System.out.println("data2(2022.4.27)是否比data3(2022.4.28)小: "+date2.isBefore(date3)); System.out.println("data2(2022.4.27)是否比data3(2022.4.28)大: "+date2.isAfter(date3));

 结果:

指定String类型的时间,和当前时间比较(常用)

讯享网 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime now = LocalDateTime.now(); String nowFormat = now.format(dateTimeFormatter); System.out.println("格式化后的当前时间:"+nowFormat); String str = "2022-03-27 14:41:06"; LocalDateTime date = LocalDateTime.parse(str, dateTimeFormatter); System.out.println("date是否比now大: "+date.isAfter(now)); String str1 = "2022-03-29 14:41:06"; LocalDateTime date1 = LocalDateTime.parse(str1, dateTimeFormatter); System.out.println("date1是否比now大: "+date1.isAfter(now));

结果:

 

 4.获取n小时之前之后时间

 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime now = LocalDateTime.now(); String nowFormat = now.format(dateTimeFormatter); System.out.println("格式化后的当前时间:"+nowFormat); //当前六小时之前时间 LocalDateTime minusTime = now.minusHours(6); String minusTimeFormat = minusTime.format(dateTimeFormatter); System.out.println("格式化后的6小时前的时间:"+minusTimeFormat); //当前六小时之后时间 LocalDateTime plusTime = now.plusHours(6); String plusTimeFormat = plusTime.format(dateTimeFormatter); System.out.println("格式化后的6小时后的时间:"+plusTimeFormat);

结果:

 

备注:LocalDate类,LocalDateTime 类是线程安全,因为类被final类型。
1,写final域的重排序规则:JMM禁止编译器把final域的写重排序到构造函数初始化之外(之后)。
编译器会在final域的写之后,构造函数return之前,插入一个StoreStore内存屏障
2,读final域的重排序规则:在一个线程中,初次读对象引用与初次读该对象包含的final域,
JMM禁止重排序这2个操作。编译器会在读final域操作的前面插入一个LoadLoad屏障。

1.一类是强制读取主内存,强制刷新主内存的内存屏障,叫做Load屏障和Store屏障
Store:将处理器缓存的数据刷新到内存中。
Load:将内存存储的数据拷贝到处理器的缓存中。
2.另外一类是禁止指令重排序的内存屏障,有四个分别叫做LoadLoad屏障、StoreStore屏障、LoadStore屏 障、StoreLoad屏障。
如:store1指令 StoreStore屏障 store2指令:在store1指令和store2指令之间加上StoreStore屏障, 强制先 执行store1指令再执行store2指令;store1指令不能和store2指令进行重排序

小讯
上一篇 2025-03-01 15:53
下一篇 2025-02-13 19:33

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/22773.html