需求:有一个时间格式:TUE NOV 14 08:00:00 GMT+08:00 2000
我需要将这种格式的时间转换为标准日期格式,并且只修改这种时间格式的时间,不影响其他的
思路:我想到的是用正则来判断,SimpleDateFormat来进行转换
代码实现
@Test
@SneakyThrows
public void test02() {
String regex = "^([A-Za-z]{3}\\s[A-Za-z]{3}\\s\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\sGMT[-+]\\d{2}:\\d{2}\\s\\d{4})$";
String timeString = "TUE NOV 14 08:00:00 GMT+08:00 2000";
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(timeString);
while (matcher.find()) {
System.out.println("================");
System.out.println(timeString);
Date date2 = inputFormat.parse(timeString);
System.out.println(date2.toString());
System.out.println(outputFormat.format(date2));
}
}
如果有更好的办法,麻烦评论区指点一下,谢谢