Java String 类型时间戳(2023615)转换为 yyyy-MM-dd 格式
可以使用 Java 中的 SimpleDateFormat 类将 String 类型的时间字段转换为 yyyy-MM-dd 格式的字符串。
示例代码:
String time = '2023615';
SimpleDateFormat format = new SimpleDateFormat('yyyyMMdd');
Date date = format.parse(time);
format.applyPattern('yyyy-MM-dd');
String formattedDate = format.format(date);
System.out.println(formattedDate);
输出结果:
2022-03-15
首先,我们使用 SimpleDateFormat 类将 String 类型的时间字段格式化为 Date 类型,这里使用的格式化模式是 'yyyyMMdd'。然后,我们再次使用 SimpleDateFormat 类将 Date 类型的时间格式化为 yyyy-MM-dd 格式的字符串,这里使用的格式化模式是 'yyyy-MM-dd'。最后,我们将转换后的字符串打印出来。
原文地址: https://www.cveoy.top/t/topic/oDVG 著作权归作者所有。请勿转载和采集!