摘要: 原创出处 blog.csdn.net/Window_mouse/article/details/116356814 「窝在小角落里学习」欢迎转载,保留摘要,谢谢!
DateTimeFormatter类
我们先来看看SImpleDateFormat
类的部分源码,如图1所示。

接着再来看看DateTimeFormatter
类的部分源码,如 图2所示。

由上可知,与SimpleDateFormat
不同的是,DateTimeFormatter
不但是不变对象,它还是线程安全的。线程的概念我们会在后面涉及到。
现在我们只需要记住:因为SimpleDateFormat
不是线程安全的,使用的时候,只能在方法内部创建新的局部变量。而DateTimeFormatter
可以只创建一个实例,到处引用。
接下来,我们来说一说DateTimeFormatter
类的常用方法
static DateTimeFormatter ofPattern(String pattern)
static DateTimeFormatter ofPattern(String pattern, Locale locale)
String format(TemporalAccessor temporal)
|
其中,TemporalAccessor
是一个接口,其实现类有LocalDate、LocalTime、LocalDateTime、ZonedDateTime
等……
所以我们在使用format
方法时,一般传入其实现类的实例化对象即可。
接下来我们举几个例子。
范例1:创建DateTimeFormatter
package edu.blog.test07;
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;
public class DateTimeFormatterTestDemo01 { public static void main(String[] args) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); System.out.println(dtf.format(LocalDateTime.now())); System.out.println("==================================="); LocalDateTime localDateTime = LocalDateTime.parse("2001/07/27 22:22:22", dtf); System.out.println(localDateTime); } }
|
由上可知,DateTimeFormatter类格式化字符串的使用方式与SImpleDateFormat一样。
此外,另一种创建DateTimeFormatter
的方法是,传入格式化字符串的同时,同时指定Locale。
范例2:按照Locale默认习惯格式化
package edu.blog.test07;
import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Locale;
public class DateTimeFormatterTestDemo02 { public static void main(String[] args) { ZonedDateTime zonedDateTime = ZonedDateTime.now(); System.out.println(zonedDateTime); System.out.println("==============================");
DateTimeFormatter formatter01 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ZZZZ"); System.out.println(formatter01.format(zonedDateTime)); System.out.println("==============================");
DateTimeFormatter formatter02 = DateTimeFormatter.ofPattern("yyyy MMM dd EE:HH:mm", Locale.CHINA); System.out.println(formatter02.format(zonedDateTime)); System.out.println("==============================");
DateTimeFormatter formatter03 = DateTimeFormatter.ofPattern("E, MMMM/dd/yyyy HH:mm", Locale.US); System.out.println(formatter03.format(zonedDateTime)); } }
|
运行本程序,分别以默认方式、中国地区和美国地区对当前时间进行显示,结果如上所述。
在格式化字符串中,如果需要输出固定字符,可以用’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)); } }
|
总结
对ZonedDateTime
或LocalDateTime
进行格式化,需要使用DateTimeFormatter
类,DateTimeFormatter
可以通过格式化字符串和Locale对日期和时间进行定制输出。