思路很简单:先转化为yyyyMMddHHmmss格式的Date类型,再格式化为yyyy-MM-dd HH:mm:ss格式的字符串类型
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConsoleTest {
public static void main(String args[]) {
String str = "20200402150221";
DateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss");
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date time1 = df1.parse(str);
String time2 = df2.format(time1);
System.out.println(time2);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
转化后结果2020-04-02 15:02:21
本文介绍了一种将日期从yyyyMMddHHmmss格式转换为更易读的yyyy-MM-dd HH:mm:ss格式的方法。通过使用Java的SimpleDateFormat类,可以轻松实现日期格式的转换。
734

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



