将iso8601字符串日期时间格式转换为Java [英] Convert iso8601 string date time format to date in Java

查看:2859
本文介绍了将iso8601字符串日期时间格式转换为Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我知道我曾经问过一个类似的问题,就是使用Java将ISO8601日期字符串解析成一个Date,但这是一个更具体的问题,使用SimpleDateFormat类。

I know I have asked a similar question on parsing an ISO8601 date string into a Date using Java before, but this is a more specific problem using the SimpleDateFormat class.

我已阅读文章维基ISO8601日期

我已经提供了一个客户的XML文件,其日期和时间格式如下:

I have been supplied an XML file from a customer which has a date and time with the following format:

2012-08-24T12:15:00 + 02:00

根据维基文章,这是有效的这是足够公平的。

According to the Wiki article this is valid which is fair enough.

给出以下代码来解析此字符串,抛出一个ParseException消息Unparseable date:2012-08-24T12:15:00 +02:00。

Given the following code to parse this string, a ParseException is thrown with the message "Unparseable date: "2012-08-24T12:15:00+02:00"".

String inputDate = "2012-08-24T12:15:00+02:00";
String format = "yyyy-MM-dd'T'HH:mm:ssz";

SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = sdf.parse(inputDate);

问题是时区说明符中的冒号。 + 02:00在时区导致异常被抛出。 +0200工作正常。

The problem is with the colon in the timezone specifier. +02:00 in the timezone causes the exception to be thrown. +0200 works fine.

问题是,是否可以使用SimpleDateFormat解析此类型的字符串?

Question is, is it possible to parse this type of string with the SimpleDateFormat?

谢谢

Andez

推荐答案

不,使用SimpleDateFormat解析此日期不可能(至少不在jdk 6或更低)。我们必须自己编写自己的适配器。

No, using SimpleDateFormat, parsing this date is not possible (at least not in jdk 6 or lower). We had to write our own adapter for this ourselves.

注意,由于此格式是XML Schema的有效部分,因此可以使用 DatatypeConverter.parseDateTime()方法来解析这个日期。

Note, since this format is a valid part of XML Schema, you can use the DatatypeConverter.parseDateTime() method to parse this date.

这篇关于将iso8601字符串日期时间格式转换为Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆