DateTimeFormatter的使用

该类在java.time.format包下,用于格式化和解析日期,类似于SimpleDateFormat。

一、常用方法

  1. ofPattern(String pattern):静态方法 ,返回一个指定字符串格式的DateTimeFormatter

  2. format(TemporalAccessor t) :格式化一个日期、时间,返回字符串

    注:LocalDate、LocalTime、LocalDateTime继承自TemporalAccessor

  3. parse(CharSequence text) :将指定格式的字符序列解析为一个日期、时间

二、格式化方法

1.预定义的标准格式

ISO_LOCAL_DATE_TIME、ISO_LOCAL_DATE、ISO_LOCAL_TIME

DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//格式化:日期-->字符串
LocalDateTime now = LocalDateTime.now();
String str1 = formatter.format(now);
System.out.println("格式化之前:"+now);//2021-07-21T18:27:42.460
System.out.println("格式化之后"+str1);//2021-07-21T18:27:42.46
//解析:字符串-->日期
TemporalAccessor parse = formatter.parse("2021-07-21T18:27:42.46");
System.out.println(parse);//{},ISO resolved to 2021-07-21T18:27:42.460

2.本地化相关的格式

(1)ofLocalizedDateTime():

FormatStyle.LONG、FormatStyle.MEDIU、FormatStyle.SHORT

注:适用于LocalDateTime

//ofLocalizedDate
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
DateTimeFormatter formatter3 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
//格式化:日期-->字符串
LocalDateTime localDateTime = LocalDateTime.now();
String str1 = formatter1.format(localDateTime);
String str2 = formatter2.format(localDateTime);
String str3 = formatter3.format(localDateTime);
System.out.println("格式化之前:"+localDateTime);//2021-07-21T18:47:28.994
System.out.println("格式化之后SHORT:"+str1);//21-7-21
System.out.println("格式化之后LONG:"+str2);//2021年7月21日
System.out.println("格式化之后MEDIUM:"+str3);//2021-7-21

//解析:字符串-->日期
TemporalAccessor parse1 = formatter1.parse("21-7-21");
TemporalAccessor parse2 = formatter2.parse("2021年7月21日");
TemporalAccessor parse3 = formatter3.parse("2021-7-21");

System.out.println(parse1);//{},ISO resolved to 2021-07-21
System.out.println(parse1);//{},ISO resolved to 2021-07-21
System.out.println(parse3);//{},ISO resolved to 2021-07-21

(2)ofLocalizedDate()

FormatStyle.FULL、FormatStyle.LONG、FormatStyle.MEDIUM、FormatStyle.SHORT

注:适用于LocalDte

//ofLocalizedDate()
DateTimeFormatter formatter4 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
//格式化
LocalDate now = LocalDate.now();
String str4 = formatter4.format(now);
System.out.println("格式化之前"+now);//2021-07-21
System.out.println("格式化后FULL:"+str4);//2021年7月21日 星期三

三、自定义的格式

ofPattern(“yyyy-MM-dd hh:mm:ss”)

注:这种方式最常用

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化:日期-->字符串
LocalDateTime now = LocalDateTime.now();
String str1 = formatter.format(now);
System.out.println(now);//2021-07-21T19:17:34.356
System.out.println(str1);//2021-07-21 07:17:34

//解析:字符串-->日期
TemporalAccessor parse = formatter.parse("2021-07-21 07:17:34");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值