我正在使用 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
谢谢,
阿南德