많은 UTC 시간 형식이 있으며 어떤 형식에 속하는지 구분해야합니다. 그렇지 않으면 오류가 발생합니다.
/* RFC 1123 with 2-digit Year */"EEE, dd MMM yy HH:mm:ss z",
/* RFC 1123 with 4-digit Year */"EEE, dd MMM yyyy HH:mm:ss z",
/* RFC 1123 with no Timezone */"EEE, dd MMM yy HH:mm:ss",
/* Variant of RFC 1123 */"EEE, MMM dd yy HH:mm:ss",
/* RFC 1123 with no Seconds */"EEE, dd MMM yy HH:mm z",
/* Variant of RFC 1123 */"EEE dd MMM yyyy HH:mm:ss",
/* RFC 1123 with no Day */"dd MMM yy HH:mm:ss z",
/* RFC 1123 with no Day or Seconds */"dd MMM yy HH:mm z",
/* ISO 8601 slightly modified */"yyyy-MM-dd'T'HH:mm:ssZ",
/* ISO 8601 slightly modified */"yyyy-MM-dd'T'HH:mm:ss'Z'",
/* ISO 8601 slightly modified */"yyyy-MM-dd'T'HH:mm:sszzzz",
/* ISO 8601 slightly modified */"yyyy-MM-dd'T'HH:mm:ss z",
/* ISO 8601 */"yyyy-MM-dd'T'HH:mm:ssz",
/* ISO 8601 slightly modified */"yyyy-MM-dd'T'HH:mm:ss.SSSz",
/* ISO 8601 slightly modified */"yyyy-MM-dd'T'HHmmss.SSSz",
/* ISO 8601 slightly modified */"yyyy-MM-dd'T'HH:mm:ss",
/* ISO 8601 w/o seconds */"yyyy-MM-dd'T'HH:mmZ",
/* ISO 8601 w/o seconds */"yyyy-MM-dd'T'HH:mm'Z'",
/* RFC 1123 without Day Name */"dd MMM yyyy HH:mm:ss z",
/* RFC 1123 without Day Name and Seconds */"dd MMM yyyy HH:mm z",
/* Simple Date Format */"yyyy-MM-dd",
/* Simple Date Format */"MMM dd, yyyy"};
UTC 시간은 세계 시간이고 베이징 시간은 UTC 시간 + 8 시간이어야합니다.
형식 : 2019-05-21T08 : 44 : 00Z
publicstaticvoidmain(String[] args)throws Exception{
Date date = null;// ISO 8601 slightly modified */"yyyy-MM-dd'T'HH:mm:ss'Z'",
SimpleDateFormat sdf =newSimpleDateFormat("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 =newSimpleDateFormat("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
publicstaticvoidmain(String[] args)throws Exception{
Date date = null;// ISO 8601 slightly modified */"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
SimpleDateFormat sdf =newSimpleDateFormat("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 =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = calendar.getTime();
String date2 = simpleDateFormat.format(calendar.getTime());
System.out.println("北京时间:"+date2);}