时区的处理: ZonedDate/ZonedTime/ZonedDateTime
package com.expgiga.Java8; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Set; /** * Java8时间日期格式化:DateTimeFormatter */ public class TestDateTimeFormatter { public static void main(String[] args) { DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME; LocalDateTime ldt = LocalDateTime.now(); String strDate = ldt.format(dtf); System.out.println(strDate); DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss"); String strDate2 = dtf2.format(ldt); System.out.println(strDate2); LocalDateTime newDate = ldt.parse(strDate2, dtf2); System.out.println(newDate); //对时区的操作ZonedDate/ZonedTime/ZonedDateTime Set<String> set = ZoneId.getAvailableZoneIds(); set.forEach(System.out::println); LocalDateTime ldt3 = LocalDateTime.now(ZoneId.of("Europe/Tallinn")); System.out.println(ldt3); LocalDateTime ldt4 = LocalDateTime.now(ZoneId.of("Asia/Shanghai")); ZonedDateTime zdt = ldt4.atZone(ZoneId.of("Asia/Shanghai")); System.out.println(zdt); } }