Java中国标准时间进行格式化的两种方法,如下
package time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.regex.Pattern;
public class FormatTransformation {
public static void main(String[] args) throws ParseException {
String metBegin = "Sat Dec 04 2021 09:18:00 GMT 0800 (中国标准时间)";
String metEnd = "Sat Dec 04 2021 09:18:00 GMT 0800 (中国标准时间)";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(metBegin !=null || metBegin.length()!=0) {
SimpleDateFormat format1 = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z", Locale.US);
String metBeginTemp = metBegin.split(Pattern.quote("(中国标准时间)"))[0].replace("GMT+0800", "GMT+08:00");
metBegin=format.format(format1.parse(metBeginTemp));
System.out.println("metBegin转换后为"+metBegin);
}
if(metEnd!=null || metEnd.length()!=0) {
String metEndTemp = metEnd.replace("GMT", "").replaceAll("\\(.*\\)", "");
SimpleDateFormat format2 = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss", Locale.ENGLISH);
metEnd=format.format(format2.parse(metEndTemp));
System.out.println("metEnd转换后为"+metEnd);
}
}
}
