一个是线程安全的,一个是线程不安全的。先来看看两者源码:(看看注释就一目了然了)
DateTimeFormatter
/* @implSpec
* This class is immutable and thread-safe.
*
* @since 1.8
*/
public final class DateTimeFormatter{...}
SimpleDateFormat
/*
* <p>
* Date formats are not synchronized.
* It is recommended to create separate format instances for each thread.
* If multiple threads access a format concurrently, it must be synchronized
* externally.
*
* @see java.util.Calendar
* @see java.util.TimeZone
* @see DateFormat
* @see DateFormatSymbols
* @author Mark Davis, Chen-Lieh Huang, Alan Liu
*/
public class SimpleDateFormat extends DateFormat {...}
再看使用示例:
DateTimeFormatter df= DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime ld=LocalDateTime.now();
System.out.println(ld.format(df.ISO_LOCAL_TIME.ISO_TIME));
DateFormat dfs = new SimpleDateFormat("yy.MM.dd HH:mm:ss");
System.out.println(dfs.format(new Date()));
System.out.println(dfs.parseObject("2020.1.4 1:2:2"));