一、String类型的日期转换为yyyyMMdd
1.方法一
public static void main(String[] args) {
String name2 = "2023-11-21 17:22:33";
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
try {
System.out.println(outputFormat.format(DateUtils.parseDate(name2, "yyyy-MM-dd HH:mm:ss")));
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
2.方法二
public static void main(String[] args) {
String name2 = "2023-11-21 17:22:33";
System.out.println(name2.substring(0, 10).replace("-", ""));
}
结果:20231121