package com.Vapour.String;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class Example28 {
public static void main(String[] args) {
LocalDateTime date = LocalDateTime.now();
System.out.println("使用常量创建DateTimeFormatter:");
DateTimeFormatter dtf1 = DateTimeFormatter.ISO_DATE_TIME;
System.out.println(dtf1.format(date));
System.out.println("使用MEDIUM类型风格的DateTimeFormatter:");
DateTimeFormatter dtf2 = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.MEDIUM);
System.out.println(dtf2.format((date)));
System.out.println("根据模式字符串来创建DateTimeFormatter:");
DateTimeFormatter dtf3 = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(date.format(dtf3));
}
}
package com.Vapour.String;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Example29 {
public static void main(String[] args) {
String str1 = "2018-01-30 12:43:34";
String str2 = "2018年01月30日 12时43分34秒";
DateTimeFormatter formatter1 = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter formatter2= DateTimeFormatter
.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
LocalDateTime localDateTime1 = LocalDateTime
.parse(str1,formatter1);
LocalDateTime localDateTime2 = LocalDateTime
.parse(str2,formatter2);
System.out.println(localDateTime1);
System.out.println(localDateTime2);
}
}