package com.msb.test02;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
/**
* 开发人:liu
* 日期:15:01:33
* 描述:IntelliJ IDEA
* 版本:1.0
*/
public class Test08 {
//这是一个main方法:是程序的入口
public static void main(String[] args) {
//格式化类:DateTimeFormatter
//方法1:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter df = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//df就可以帮助我们完成LocalDateTime和String之间的相互转换
//localTateTime--->string
LocalDateTime now = LocalDateTime.now();
String st=df.format(now);
System.out.println(st);//2022-10-14T15:52:13.152
System.out.println("-----String--->LocalDateTime-----");
//将String转为LocalDateTime
TemporalAccessor parse = df.parse("2022-10-14T15:52:13.152");
System.out.println(parse);
System.out.println("---------------------");
//方法2:本地化相关的格式。如:oflocalizedDateTime()
//参数:FormatStyle.LONG FormatStyle.MEDIUM FormatStyle.SHORT
//FormatStyle.LONG 2022年10月14日 下午04时54分37秒
//FormatStyle.MEDIUM 2022-10-14 16:57:52
//FormatStyle.SHORT 22-10-14 下午4:58
DateTimeFormatter df1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
//LocalDateTime--->String
LocalDateTime now1 = LocalDateTime.now();
System.out.println(df1.format(now1));
//String---->LocalDateTime
TemporalAccessor now2=df1.parse("22-10-14 下午4:58");
System.out.println("--------------------");
//方法3:自定义的格式:如ofpattern("yyyy-mm-dd hh:mm:ss")--->重点,以后经常用
DateTimeFormatter df3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
LocalDateTime now3 = LocalDateTime.now();
//LocalDateTime---->Sting
String format=df3.format(now3);
System.out.println(format);
//String---->LocalDateTime
TemporalAccessor parse1 = df3.parse("2022-10-14 05:30:30");
System.out.println(parse1);
}
}