public class DateUtil {
public static String toFormatDate(String strDate) {
try {
Date date = new SimpleDateFormat("yyyyMMdd").parse(strDate);
return new SimpleDateFormat("yyyy-MM-dd").format(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}
@Test
public void dateConvertion() {
String str = "20200201";
String string = "2020-02-01";
try {
Date format1 = new SimpleDateFormat("yyyyMMdd").parse(str);
String longDate = new SimpleDateFormat("yyyy-MM-dd").format(format1);
System.out.println("yyyyMMdd转yyyy-MM-dd:"+longDate);
Date format2 = new SimpleDateFormat("yyyy-MM-dd").parse(string);
String shortDate = new SimpleDateFormat("yyyyMMdd").format(format2);
System.out.println("yyyy-MM-dd转yyyyMMdd:"+shortDate);
} catch (ParseException e) {
throw new BizException("转换异常");
}
}