2013-04-03 94 views
0

我正在使用Joda時間來驗證帶時區的時間戳。當我通過無效的日期或時間,它按預期工作。例如,當我通過99秒作爲它給了以下錯誤:Jodatime時區轉換

Exception in thread "main" org.joda.time.IllegalFieldValueException: Cannot 
parse "20131231235999+00": Value 99 for secondOfMinute must be in the range [0,59] 

,我希望它拋出一個異常相似與我通過非法的時區也是如此。 我期待UTC偏差值小於-12且大於+14小時(http://en.wikipedia.org/wiki/List_of_time_zones_by_UTC_offset)。當偏移值超過23小時時,確實出現以下錯誤。

Exception in thread "main" java.lang.IllegalArgumentException: Invalid 
format: "20131231235959+24" is malformed at "24" 

我想我錯過了一些東西,因爲它指出了24小時的錯誤。有人可以解釋爲什麼它允許偏移值< -12和> 14小時。

下面給出的是與不同的偏移輸出的一個範例程序:

public static void main(String[] args) throws ParseException, DatatypeConfigurationException 
{ 
    DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyyMMddHHmmssZ").withZoneUTC(); 
    String dateString = ""; 

    for (int i = 0; i < 24; i++) 
    { 
     if (i < 10) 
     { 
      dateString = "20131231235959+0" + i; 
      DateTime dt1 = dtf.parseDateTime(dateString); 
      System.err.println(dateString + " = " + dt1); 
     } 
     else 
     { 
      dateString = "20131231235959+" + i; 
      DateTime dt1 = dtf.parseDateTime(dateString); 
      System.err.println(dateString + " = " + dt1); 
     } 
    } 


    for (int i = 0; i < 24; i++) 
    { 
     if (i < 10) 
     { 
      dateString = "20131231235959-0" + i; 
      DateTime dt1 = dtf.parseDateTime(dateString); 
      System.err.println(dateString + " = " + dt1); 
     } 
     else 
     { 
      dateString = "20131231235959-" + i; 
      DateTime dt1 = dtf.parseDateTime(dateString); 
      System.err.println(dateString + " = " + dt1); 
     } 
    } 
} 

Output: 

20131231235959+00 = 2013-12-31T23:59:59.000Z 20131231235959-00 = 2013-12-31T23:59:59.000Z 
20131231235959+01 = 2013-12-31T22:59:59.000Z 20131231235959-01 = 2014-01-01T00:59:59.000Z 
20131231235959+02 = 2013-12-31T21:59:59.000Z 20131231235959-02 = 2014-01-01T01:59:59.000Z 
20131231235959+03 = 2013-12-31T20:59:59.000Z 20131231235959-03 = 2014-01-01T02:59:59.000Z 
20131231235959+04 = 2013-12-31T19:59:59.000Z 20131231235959-04 = 2014-01-01T03:59:59.000Z 
20131231235959+05 = 2013-12-31T18:59:59.000Z 20131231235959-05 = 2014-01-01T04:59:59.000Z 
20131231235959+06 = 2013-12-31T17:59:59.000Z 20131231235959-06 = 2014-01-01T05:59:59.000Z 
20131231235959+07 = 2013-12-31T16:59:59.000Z 20131231235959-07 = 2014-01-01T06:59:59.000Z 
20131231235959+08 = 2013-12-31T15:59:59.000Z 20131231235959-08 = 2014-01-01T07:59:59.000Z 
20131231235959+09 = 2013-12-31T14:59:59.000Z 20131231235959-09 = 2014-01-01T08:59:59.000Z 
20131231235959+10 = 2013-12-31T13:59:59.000Z 20131231235959-10 = 2014-01-01T09:59:59.000Z 
20131231235959+11 = 2013-12-31T12:59:59.000Z 20131231235959-11 = 2014-01-01T10:59:59.000Z 
20131231235959+12 = 2013-12-31T11:59:59.000Z 20131231235959-12 = 2014-01-01T11:59:59.000Z 
20131231235959+13 = 2013-12-31T10:59:59.000Z 20131231235959-13 = 2014-01-01T12:59:59.000Z 
20131231235959+14 = 2013-12-31T09:59:59.000Z 20131231235959-14 = 2014-01-01T13:59:59.000Z 
20131231235959+15 = 2013-12-31T08:59:59.000Z 20131231235959-15 = 2014-01-01T14:59:59.000Z 
20131231235959+16 = 2013-12-31T07:59:59.000Z 20131231235959-16 = 2014-01-01T15:59:59.000Z 
20131231235959+17 = 2013-12-31T06:59:59.000Z 20131231235959-17 = 2014-01-01T16:59:59.000Z 
20131231235959+18 = 2013-12-31T05:59:59.000Z 20131231235959-18 = 2014-01-01T17:59:59.000Z 
20131231235959+19 = 2013-12-31T04:59:59.000Z 20131231235959-19 = 2014-01-01T18:59:59.000Z 
20131231235959+20 = 2013-12-31T03:59:59.000Z 20131231235959-20 = 2014-01-01T19:59:59.000Z 
20131231235959+21 = 2013-12-31T02:59:59.000Z 20131231235959-21 = 2014-01-01T20:59:59.000Z 
20131231235959+22 = 2013-12-31T01:59:59.000Z 20131231235959-22 = 2014-01-01T21:59:59.000Z 
20131231235959+23 = 2013-12-31T00:59:59.000Z 20131231235959-23 = 2014-01-01T22:59:59.000Z 

感謝,

阿南德

+0

我有一種感覺,它與「因爲這是目前的範圍,它可能並不總是」心態。由於已經存在大於12的絕對偏移量,因此任何小於一整天的值都是有效的。然而,絕對偏差24或更大將是非常奇怪的,但是(特別是因爲這不會是'一整天'......) –

回答

0

從ISO 8601:

When it is required to indicate the difference between local time and UTC of day, the representation of the difference can be expressed in hours and minutes, or hours only. It shall be expressed as positive (i.e. with the leading plus sign [+]) if the local time is ahead of or equal to UTC of day and as negative (i.e. with the leading minus sign [-]) if it is behind UTC of day. The minutes time element of the difference may only be omitted if the difference between the time scales is exactly an integral number of hours. Basic format: ±hhmm Example: +0100 ±hh +01 Extended format: ±hh:mm Example: +01:00

它沒有明確提到一個範圍,但他們確實使用相同的字母幾個小時。鑑於joda time uses ISO 8601 by default似乎暗示偏移的範圍與時間表示的小時值的範圍相同。