今天在跟前端联调接口的时候,发现前端显示的时间格式有点问题,想将其转为正确的时间格式,于是抽取了一个工具方法。
/**
* 时间格式转换工具
*
* @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());
}
}