java Stirng类型的202391如何转换yyyy-MM-dd格式
可以使用SimpleDateFormat类来将String类型的日期转换为指定格式的日期。示例代码如下:
String dateStr = "202391";
DateFormat sourceFormat = new SimpleDateFormat("yyyyMMdd");
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sourceFormat.parse(dateStr);
String targetDateStr = targetFormat.format(date);
System.out.println(targetDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
输出结果为:2023-09-01
原文地址: https://www.cveoy.top/t/topic/haN8 著作权归作者所有。请勿转载和采集!