java8新特性_20_时间格式化与时区处理
程序员文章站
2022-05-05 08:56:03
...
@Test
public void test2(){
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
LocalDateTime ldt = LocalDateTime.now();
String strDate = ldt.format(dtf);
System.out.println(strDate);//2019-09-27
DateTimeFormatter dtf2 = DateTimeFormatter.ISO_DATE_TIME;
System.out.println(ldt.format(dtf2));//2019-09-27T08:34:51.378
DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String strDate2 = ldt.format(dtf3);
System.out.println(strDate2);//2019-09-27 08:34:51
LocalDateTime newDate = LocalDateTime.parse(strDate2, dtf3);
System.out.println(newDate);//2019-09-27T08:38:09
}
//支持的时区
@Test
public void test3(){
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
availableZoneIds.forEach(System.out::println);
}
//带时区的时间
@Test
public void test4(){
LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
System.out.println(ldt);
LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
ZonedDateTime zdt = localDateTime.atZone(ZoneId.of("Europe/Tallinn"));
System.out.println(zdt);
}