public class DateFormat {
public static void main(String[] args) {
Date date = new Date();
// 输出为:Wed Aug 17 20:48:38 CST 2022
System.out.println(date);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format1 = format.format(date);
// Date转换为yyyy-MM-dd HH:mm:ss的字符串
// 输出为:2022-08-17 20:48:38
System.out.println(format1);
try {
// 字符串类型的yyyy-MM-dd HH:mm:ss,转换为Date
Date format2 = format.parse(format1);
// 输出为 Wed Aug 17 20:48:38 CST 2022
System.out.println(format2);
} catch (ParseException e) {
e.printStackTrace();
}
}
}