HttpDate GMT时间和UTC时间

原创
2018/06/28 14:37
阅读数 6.9K

时间标准简介

UTC(世界标准时间)

协调世界时,又称世界标准时间或世界协调时间,简称UTC(从英文“Coordinated Universal Time”/法文“Temps 
Universel Coordonné”而来),是最主要的世界时间标准,其以原子时秒长为基础,在时刻上尽量接近于格林尼治标准时间。

GMT(格林尼治平时)

格林尼治平时(又称格林尼治平均时间或格林尼治标准时间,旧译格林威治标准时间;英语:Greenwich Mean 
Time,GMT)是指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。

理论上来说,格林尼治标准时间的正午是指当太阳横穿格林尼治子午线时(也就是在格林尼治上空最高点时)的时间。由于地球在它的椭圆轨道里的运动速度不均匀,这个时刻可能与实际的太阳时有误差,最大误差达16分钟。 
由于地球每天的自转是有些不规则的,而且正在缓慢减速,因此格林尼治时间已经不再被作为标准时间使用。现在的标准时间,是由原子钟报时的协调世界时(UTC)。

CST(北京时间)

北京时间,China Standard Time,中国标准时间。在时区划分上,属东八区,比协调世界时早8小时,记为UTC+8。

不过这个CST这个缩写比较纠结的是它可以同时代表四个不同的时间: Central Standard Time (USA) UT-6:00 
Central Standard Time (Australia) UT+9:30 China Standard Time UT+8:00 
Cuba Standard Time UT-4:00


Java Date使用UTC时间,如 Tue Jan 05 14:28:41 CST 2016 表示China Standard Time UT+8:00 。

Java时间处理

日期和时间模式 
日期和时间格式由日期和时间模式 字符串指定。在日期和时间模式字符串中,未加引号的字母 ‘A’ 到’Z’ 和’a’ 到’z’ 被解释为模式字母,用来表示日期或时间字符串元素。文本可以使用单引号 (‘) 引起来,以免进行解释。””” 表示单引号。所有其他字符均不解释;只是在格式化时将它们简单复制到输出字符串,或者在解析时与输入字符串进行匹配。 
定义了以下模式字母(所有其他字符’A’ 到’Z’ 和’a’ 到’z’ 都被保留):

字母 日期或时间元素 类型 示例
G Era 标志符 Text AD
y 年份 Number 1996; 96
M 年份中的月份 Text July; Jul; 07
w 年份中的周数 Number 27
W 月份中的周数 Number 2
D 年份中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text Tuesday; Tue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
Z 时区 RFC 822 time zone -0800

 

在实际开发过程中经常会遇到将Date类型的数据转换为String 类型或将String 类型的日期转换成Date类型的问题。下面就介绍一下如何将String类型的GMT、GST日期转换成Date对象。 
JDK中提供了SimpleDateFormat类来实现String类型的日期和Date对象之间的互转。 
1.GMT时间转换

//字符串转Date
String stringDate = "Thu Oct 16 07:13:48 GMT 2015";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM ddHH:mm:ss 'GMT' yyyy",Locale.US);
Date date =sdf.parse(stringDate);
System.out.println(date.toString());

//Date转字符串
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
System.out.println(sdf.format(new Date()));

2.

//格式化时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String time = sdf.format(new Date());
System.out.println(time);

//解析时间 2016-01-05T15:06:58+0800
Date date = sdf.parse(time);
System.out.println(date);

3.

//T代表后面跟着时间,Z代表UTC统一时间
//格式化时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String time = sdf.format(new Date());
System.out.println(time);

//解析时间 2016-01-05T15:09:54Z
Date date = sdf.parse(time);
System.out.println(date);

同理,我们使用Joda-Time实现。

 

服务器时间转换

服务器一半使用GMT时间来进行资源传输,而不是使用UTC时区划分的方法,这种好处是浏览器只做单独的校验,类似UTF一样,全世界通用。

public final class HttpDate {
  /** The last four-digit year: "Fri, 31 Dec 9999 23:59:59 GMT". */
  public static final long MAX_DATE = 253402300799999L;
  public static final TimeZone UTC = TimeZone.getTimeZone("GMT"); //GMT时区

  /**
   * Most websites serve cookies in the blessed format. Eagerly create the parser to ensure such
   * cookies are on the fast path.
   */
  private static final ThreadLocal<DateFormat> STANDARD_DATE_FORMAT =
      new ThreadLocal<DateFormat>() {
        @Override protected DateFormat initialValue() {
          // RFC 2616 specified: RFC 822, updated by RFC 1123 format with fixed GMT.
          //注意格式化时必须带有 'GMT',否则无法转为GMT时间,参考SimpleDateFormat parse方法
          DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
          rfc1123.setLenient(false);
          rfc1123.setTimeZone(UTC);//设置时区
          return rfc1123;
        }
      };

  /** If we fail to parse a date in a non-standard format, try each of these formats in sequence. */
  private static final String[] BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS = new String[] {
      // HTTP formats required by RFC2616 but with any timezone.
      "EEE, dd MMM yyyy HH:mm:ss zzz", // RFC 822, updated by RFC 1123 with any TZ
      "EEEE, dd-MMM-yy HH:mm:ss zzz", // RFC 850, obsoleted by RFC 1036 with any TZ.
      "EEE MMM d HH:mm:ss yyyy", // ANSI C's asctime() format
      // Alternative formats.
      "EEE, dd-MMM-yyyy HH:mm:ss z",
      "EEE, dd-MMM-yyyy HH-mm-ss z",
      "EEE, dd MMM yy HH:mm:ss z",
      "EEE dd-MMM-yyyy HH:mm:ss z",
      "EEE dd MMM yyyy HH:mm:ss z",
      "EEE dd-MMM-yyyy HH-mm-ss z",
      "EEE dd-MMM-yy HH:mm:ss z",
      "EEE dd MMM yy HH:mm:ss z",
      "EEE,dd-MMM-yy HH:mm:ss z",
      "EEE,dd-MMM-yyyy HH:mm:ss z",
      "EEE, dd-MM-yyyy HH:mm:ss z",

      /* RI bug 6641315 claims a cookie of this format was once served by www.yahoo.com */
      "EEE MMM d yyyy HH:mm:ss z",
  };

 //浏览器兼容格式
  private static final DateFormat[] BROWSER_COMPATIBLE_DATE_FORMATS =
      new DateFormat[BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS.length];

  /** Returns the date for {@code value}. Returns null if the value couldn't be parsed. */
  public static Date parse(String value) {
    if (value.length() == 0) {
      return null;
    }

    ParsePosition position = new ParsePosition(0);
    Date result = STANDARD_DATE_FORMAT.get().parse(value, position);
    if (position.getIndex() == value.length()) {
      // STANDARD_DATE_FORMAT must match exactly; all text must be consumed, e.g. no ignored
      // non-standard trailing "+01:00". Those cases are covered below.
      return result;
    }
    synchronized (BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS) {
      for (int i = 0, count = BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS.length; i < count; i++) {
        DateFormat format = BROWSER_COMPATIBLE_DATE_FORMATS[i];
        if (format == null) {
          format = new SimpleDateFormat(BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS[i], Locale.US);
          // Set the timezone to use when interpreting formats that don't have a timezone. GMT is
          // specified by RFC 2616.
          format.setTimeZone(UTC);
          BROWSER_COMPATIBLE_DATE_FORMATS[i] = format;
        }
        position.setIndex(0);
        result = format.parse(value, position);
        if (position.getIndex() != 0) {
          // Something was parsed. It's possible the entire string was not consumed but we ignore
          // that. If any of the BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS ended in "'GMT'" we'd have
          // to also check that position.getIndex() == value.length() otherwise parsing might have
          // terminated early, ignoring things like "+01:00". Leaving this as != 0 means that any
          // trailing junk is ignored.
          return result;
        }
      }
    }
    return null;
  }

  /** 事件转为格林尼治时间*/ 
  public static String format(Date value) {
    return STANDARD_DATE_FORMAT.get().format(value);
  }

  private HttpDate() {
  }
}

 

 

展开阅读全文
加载中
点击加入讨论🔥(1) 发布并加入讨论🔥
1 评论
1 收藏
1
分享
返回顶部
顶部