简单的东西不解释直接上代码:
StringBuffer s=new StringBuffer("20190305").insert(4,"-").insert(7,"-");
System.out.println(s);
=============================================================================
public String strToDateFormat(String date) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
formatter.setLenient(false);
Date newDate= formatter.parse(date);
formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(newDate);
}
try {
System.out.println(this.strToDateFormat("20190305"));
} catch (ParseException e) {
e.printStackTrace();
}
本文介绍了一种使用Java进行日期格式转换的方法,通过示例代码展示了如何将'yyyyMMdd'格式的日期字符串转换为'yyyy-MM-dd'格式。首先利用StringBuffer进行插入操作,然后通过SimpleDateFormat解析和重新格式化日期。
1824

被折叠的 条评论
为什么被折叠?



