package com.zs.boot.controller; import org.junit.Test; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; import java.util.Set; public class TestlocalDateTime { //5. DateTimeFormatter : 解析和格式化日期或时间 @Test public void test(){ //默认的 //DateTimeFormatter dtf = DateTimeFormatter.ISO_LOCAL_DATE; //我们自己指定的格式 //DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss E"); LocalDateTime ldt = LocalDateTime.now(); String strDate = ldt.format(dtf); System.out.println(strDate);//2021年12月05日 11:39:14 星期日 LocalDateTime newLdt = ldt.parse(strDate, dtf); System.out.println(newLdt);//2021-12-05T11:42:10 } @Test public void test6(){ //获取所有时区时区信息 Set<String> set = ZoneId.getAvailableZoneIds(); set.forEach(System.out::println); } //6.ZonedDate、ZonedTime、ZonedDateTime :带时区的时间或日期 @Test public void test7(){ LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai")); System.out.println(ldt);//2021-12-05T11:53:32.613 LocalDateTime ldt1 = LocalDateTime.now(ZoneId.of("Asia/Shanghai")); ZonedDateTime zz = ldt1.atZone(ZoneId.of("Asia/Shanghai")); System.out.println(zz);//2021-12-05T11:53:32.614+08:00[Asia/Shanghai] ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("US/Pacific")); System.out.println(zdt);//2021-12-04T19:53:32.628-08:00[US/Pacific] } }