二者区别:SimpleDateFormat和Date()是线程不安全的。并且效率比较低。 而DateTimeFormatter是1.8版本新实现的时间类,尽量使用DateTimeFormatter表示时间。
使用方法:
// 1. 获取日期
Date date = new Date();
// 2. 时间转换
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(formatter.format(date));
// 1. 获取当前的日期
LocalDateTime currentTime = LocalDateTime.now();
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
// 2. 时间转换
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println("当前时间转换"+ currentTime .format(formatter));
关于DateTimeFormatter时间工具类的复用:https://blog.csdn.net/tanhongwei1994/article/details/86680845
关于SimpleDateFormat线程不安全的详细解释:https://blog.csdn.net/qq_35764295/article/details/108369391



