//date转String类型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
String formatStr = sdf.format(date);
System.out.println(formatStr);
2.Java时间 String转Date
//String转date类型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatStr = "2022-05-31 13:54:20";
Date timeDate=null;
try {
timeDate = sdf.parse(formatStr);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(timeDate);
运行结果如下:



