Java8新时间API
2022/7/5 1:20:13
本文主要是介绍Java8新时间API,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import org.junit.Test; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalAdjusters; import java.util.Set; //Arbor 2022/7/4 public class NewTime { @Test public void test1(){ LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("当前系统时间:"+localDateTime); LocalDateTime localDateTime3 = localDateTime.minusDays(1); System.out.println("减一天"+localDateTime3); // Instant() unix元年,1970年1月1日00:00:00,这个不是系统时间 Instant ins1 = Instant.now(); System.out.println("utc时间"+ins1); // 这是utc时间和utc差8小时见差 OffsetDateTime odt = ins1.atOffset(ZoneOffset.ofHours(8)); System.out.println("utc时间+8小时"+odt); System.out.println("1970年1月1日00:00:00至今毫秒值"+ins1.toEpochMilli());//毫秒值 } @Test public void test2(){ Instant ins1 = Instant.now(); Instant ins2 = Instant.now(); // Duration计算两个时间之间间隔 // Period计算两个日期之间间隔的 // 计算机的时间 Duration between1 = Duration.between(ins1, ins2); System.out.println(between1.toMillis()); // 人看的时间 LocalDateTime localDateTime1 = LocalDateTime.now(); LocalDateTime localDateTime2 = LocalDateTime.now(); Duration between2 = Duration.between(localDateTime1, localDateTime2); System.out.println(between2.toMillis()); LocalDate localDate1 = LocalDate.of(2022,2,2); LocalDate localDate2 = LocalDate.now(); Period period = Period.between(localDate1,localDate2); System.out.println(period.getDays()); } @Test public void test3(){ // 时间矫正器,设置下一个月,下一个星期几。。 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime); // 指定月的天某一天 LocalDateTime withDayOfMonth = localDateTime.withDayOfMonth(10); System.out.println("指定月的天某一天:"+withDayOfMonth); LocalDateTime with = localDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)); System.out.println("下一个SUNDAY:"+with); LocalDateTime with1 = localDateTime.with((l) -> { LocalDateTime ldt4 = (LocalDateTime) l; DayOfWeek dayOfWeek = ldt4.getDayOfWeek(); System.out.println(dayOfWeek); return ldt4.plusDays(1); }); System.out.println(with1); } @Test public void Test4(){ // 时间格式化 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE; LocalDateTime ldt = LocalDateTime.now(); String format = ldt.format(dateTimeFormatter); System.out.println(format); // 自定义 DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String format1 = ldt.format(dateTimeFormatter1); System.out.println(format1); //------------------------------------- String format2 = dateTimeFormatter1.format(ldt); System.out.println(format2); } @Test public void Test5(){ // 时区 Set<String> availableZoneIds = ZoneId.getAvailableZoneIds(); // System.out.println("支持的时区:"); // for (String s:availableZoneIds) { // System.out.println(s); // } LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai")); System.out.println("指定时区的时间"+now); ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Shanghai")); System.out.println("指定时区的时间格式时间"+zonedDateTime); } }
这篇关于Java8新时间API的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?