今天在跟前端联调接口的时候,发现前端显示的时间格式有点问题,想将其转为正确的时间格式,于是抽取了一个工具方法。
/**
* 时间格式转换工具
*
* @param beforeTime 2022-09-04T08:56:35.000+0000
* @return 2022-09-04 08:56:35
*/
public static String timeFormat(String beforeTime) {
try {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf1.parse(beforeTime);
return sdf2.format(date);
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
这篇博客介绍了在前端与后台交互中遇到的时间格式问题,以及如何解决这个问题。作者提供了一个Java工具方法,用于将ISO 8601格式的时间字符串转换为更常见的日期时间格式。此方法使用了`SimpleDateFormat`进行日期的解析和格式化。

724

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



