日期转字符串
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
System.out.println(sdf.format(date));
运行结果
2021-11-09
2021-11-09 14:42:08
2021年11月09日 14:42:08
字符串转日期
String string = "2021-11-09 14:42:08";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.parse(string));
运行结果
Mon Nov 09 00:00:00 CST 2021