package demo;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeDemo {
public static void main(String[] args) {
LocalDate ld1 = LocalDate.now();
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String str1 = ld1.format(dtf1);
LocalDate ld2 = LocalDate.parse(str1, dtf1);
System.out.println("Case1");
System.out.println("LocalDate : " + ld1);
System.out.println("LocalDate >> String : " + str1);
System.out.println("String >> LocalDate : " + ld2);
System.out.println();
LocalDateTime ldt1 = LocalDateTime.now();
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String str2 = ldt1.format(dtf2);
LocalDateTime ldt2 = LocalDateTime.parse(str2, dtf2);
System.out.println("Case2");
System.out.println("LocalDateTime : " + ldt1);
System.out.println("LocalDateTime >> String : " + str2);
System.out.println("String >> LocalDateTime : " + ldt2);
System.out.println();
}
}
标签:String,LocalDateTimeDemo,System,println,日期,LocalDateTime,字符串,LocalDate,out
From: https://www.cnblogs.com/renguanyu/p/18211416