我正在使用Joda时间来验证时区的时间戳。当我通过无效的日期或时间时,它按预期工作。例如,当我以秒为单位传递99时,它会出现以下错误:
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 :(得分:0)
来自ISO 8601:
当需要指出当地时间和当天的UTC之间的差异时,表示 差异可以用小时和分钟表示,或仅以小时表示。它应表示为正(即 如果当地时间超过或等于当天的UTC并且为负数(即带有...),则为前导加号[+] 领先减号[ - ])如果它在UTC的日期之后。差异的分钟时间元素可能只是 如果时间尺度之间的差异恰好是整数小时,则省略。 基本格式:±hhmm示例:+0100 ±hh +01 扩展格式:±hh:mm示例:+01:00
它没有明确提及范围,但它们使用相同的字母和小时。鉴于joda time uses ISO 8601 by default似乎暗示偏移的范围与时间表示的小时值的范围相同。