Date的toString字符串转换成指定日期格式 Mon Mar 01 15:09:46 GMT+08:00 2021
程序员文章站
2022-07-08 15:45:54
代码如下import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import java.util.regex.Pattern;public class Test1 { public static void main(String[] args) { //这个是要转换的字符串 String dat="Mon Mar 01 15:09:46 GMT+08:00...
代码如下
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;
public class Test1 {
public static void main(String[] args) {
//这个是要转换的字符串
String dat="Mon Mar 01 15:09:46 GMT+08:00 2021";
System.out.println(parseTimeZone(dat));
}
public static String parseTimeZone(String dateString) {
String FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
String FORMAT_STRING2 = "EEE MMM dd HH:mm:ss z yyyy";
String[] REPLACE_STRING = new String[]{"GMT+0800", "GMT+08:00"};
String SPLIT_STRING = "(中国标准时间)";
try {
dateString = dateString.split(Pattern.quote(SPLIT_STRING))[0].replace(REPLACE_STRING[0], REPLACE_STRING[1]);
//转换为date
SimpleDateFormat sf1 = new SimpleDateFormat(FORMAT_STRING2, Locale.ENGLISH);
Date date = sf1.parse(dateString);
return new SimpleDateFormat(FORMAT_STRING).format(date);
} catch (Exception e) {
throw new RuntimeException("时间转化格式错误" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");
}
}
}
本文地址:https://blog.csdn.net/weixin_43620015/article/details/114265878