请别再使用 SimpleDateFormat 格式化时间了,DateTimeFormatter 更出色!
时间:2023-03-14 18:00:00
点击关注微信官方账号,实用技术文章及时了解
DateTimeFormatter类
让我们先SImpleDateFormat
如图1所示。
然后再看DateTimeFormatter
类的部分源代码,如 图2所示。
由上可知,和SimpleDateFormat
不同的是,DateTimeFormatter
它不仅是一个不变的对象,而且是一个安全的线程。我们将在后面讨论线程的概念。
现在我们只需要记住:因为SimpleDateFormat
线程不安全,使用时只能在方法内部创建新的局部变量。DateTimeFormatter
只能创建一个例子,到处引用。
接下来,我们来谈谈DateTimeFormatter
类的常用方法
///用指定的模式创建格式化程序 staticDateTimeFormatterofPattern(Stringpattern) ///用指定的模式创建格式化程序和现场。 staticDateTimeFormatterofPattern(Stringpattern,Localelocale) //使用此格式化程序格式的日期时间对象 Stringformat(TemporalAccessortemporal)
其中,TemporalAccessor
是一个接口,其实现类有LocalDate、LocalTime、LocalDateTime、ZonedDateTime
等……
所以我们在用format
在方法中,一般可以将实例对象引入实际类别。
接下来我们举几个例子。
范例1:创建DateTimeFormatter
packageedu.blog.test07; importjava.time.LocalDateTime; importjava.time.format.DateTimeFormatter; publicclassDateTimeFormatterTestDemo01{ publicstaticvoidmain(String[]args){ ///自定义输出格式 DateTimeFormatterdtf=DateTimeFormatter.ofPattern("yyyy/MM/ddHH:mm:ss"); System.out.println(dtf.format(LocalDateTime.now())); System.out.println("==================================="); ///自定义格式分析 LocalDateTimelocalDateTime=LocalDateTime.parse("2001/07/2722:22:22",dtf); System.out.println(localDateTime); } } /* 结果: 2021/04/0223:14:46 =================================== 2001-07-27T22:22:22 */
由上可知,DateTimeFormatter使用格式化字符串的方式SImpleDateFormat一样。
另外,另一种创造DateTimeFormatter
方法是在引入格式化字符串的同时指定Locale。
范例2:按照Locale默认习惯格式化
packageedu.blog.test07; importjava.time.ZonedDateTime; importjava.time.format.DateTimeFormatter; importjava.util.Locale; publicclassDateTimeFormatterTestDemo02{ publicstaticvoidmain(String[]args){ ZonedDateTimezonedDateTime=ZonedDateTime.now(); System.out.println(zonedDateTime); System.out.println("=============================="); DateTimeFormatterformatter01=DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ZZZZ"); System.out.println(formatter01.format(zonedDateTime)); System.out.println("=============================="); DateTimeFormatterformatter02=DateTimeFormatter.ofPattern("yyyyMMMddEE:HH:mm",Locale.CHINA); System.out.println(formatter02.format(zonedDateTime)); System.out.println("=============================="); DateTimeFormatterformatter03=DateTimeFormatter.ofPattern("E,MMMM/dd/yyyyHH:mm",Locale.US); System.out.println(formatter03.format(zonedDateTime)); } } /* 结果: 2021-04-02T23:27:59.326 08:00[Asia/Shanghai] ============================== 2021-04-02T23:27:GMT 08:00 ============================== 2021四月02星期五:23:27 ============================== Fri,April/02/202123:27 */
本程序的运行分别以默认、中国和美国的方式显示当前时间,结果如上所述。
在格式化字符串中,如果需要输出固定字符,可以使用xxx’表示。
当我们直接调用时"System.out.println()
"对一个ZonedDateTime
或者LocalDateTime
实例进行打的时候,实际上,调用的是它们的toString()
方法,默认的toString()
方法显示的字符串就是按照ISO 8601
格式显示的,我们可以通过DateTimeFormatter
预定义的几个静态变量来引用。
范例3:过DateTimeFormatter预定义静态变量
package edu.blog.test07;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterTestDemo03 {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
System.out.println(DateTimeFormatter.ISO_DATE.format(localDateTime));
System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(localDateTime));
}
}
/*
结果:
2021-04-02T23:38:11.707
2021-04-02
2021-04-02T23:38:11.707
*/
总结
对ZonedDateTime
或LocalDateTime
进行格式化,需要使用DateTimeFormatter
类,DateTimeFormatter
可以通过格式化字符串和Locale对日期和时间进行定制输出。
来源:blog.csdn.net/Window_mouse/article/
details/116356814?
推荐
Java面试题宝典
技术内卷群,一起来学习!!
PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我们吧!