UTC时间格式有很多,需要辨清自己是属于哪种格式,否则会出错
"EEE, dd MMM yy HH:mm:ss z",
"EEE, dd MMM yyyy HH:mm:ss z",
"EEE, dd MMM yy HH:mm:ss",
"EEE, MMM dd yy HH:mm:ss",
"EEE, dd MMM yy HH:mm z",
"EEE dd MMM yyyy HH:mm:ss",
"dd MMM yy HH:mm:ss z",
"dd MMM yy HH:mm z",
"yyyy-MM-dd'T'HH:mm:ssZ",
"yyyy-MM-dd'T'HH:mm:ss'Z'",
"yyyy-MM-dd'T'HH:mm:sszzzz",
"yyyy-MM-dd'T'HH:mm:ss z",
"yyyy-MM-dd'T'HH:mm:ssz",
"yyyy-MM-dd'T'HH:mm:ss.SSSz",
"yyyy-MM-dd'T'HHmmss.SSSz",
"yyyy-MM-dd'T'HH:mm:ss",
"yyyy-MM-dd'T'HH:mmZ",
"yyyy-MM-dd'T'HH:mm'Z'",
"dd MMM yyyy HH:mm:ss z",
"dd MMM yyyy HH:mm z",
"yyyy-MM-dd",
"MMM dd, yyyy"};
UTC时间是世界时间,北京时间应在UTC时间+上8小时
public static void main(String[] args) throws Exception{
Date date = null;
// ISO 8601 slightly modified */"yyyy-MM-dd'T'HH:mm:ss'Z'",
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
// 2019-05-21T08:44:00Z 对应的时间格式 yyyy-MM-dd'T'HH:mm:ss'Z'
date = sdf.parse("2019-05-21T08:44:00Z");
System.out.println("UTC时间:"+date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR,calendar.get(Calendar.HOUR)+8);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = calendar.getTime();
String date2 = simpleDateFormat.format(calendar.getTime());
System.out.println("北京时间:"+date2);
}
- 格式: 2020-09-18T06:44:32.552Z
public static void main(String[] args) throws Exception{
Date date = null;
// ISO 8601 slightly modified */"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
// 2020-09-18T06:44:32.552Z 对应的时间格式 yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
date = sdf.parse("2020-09-18T06:44:32.552Z");
System.out.println("UTC时间:"+date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR,calendar.get(Calendar.HOUR)+8);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = calendar.getTime();
String date2 = simpleDateFormat.format(calendar.getTime());
System.out.println("北京时间:"+date2);
}