java 将字符串转换为日期,快完成了!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6279647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:10:44  来源:igfitidea点击:

Converting a String to Date, almost done!

javadatepaypal

提问by Cyril N.

Possible Duplicate:
Converting ISO8601-compliant String to java.util.Date

可能的重复:
将符合 ISO8601 的字符串转换为 java.util.Date

I'm trying to convert this String:

我正在尝试转换此字符串:

2011-06-07T14:08:59.697-07:00

To a Java Date, so far, here's what I did:

对于 Java 日期,到目前为止,这是我所做的:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
Date date1 = sdf.parse("2011-06-07T14:08:59.697", new java.text.ParsePosition(0));

Almost everything is good, except the most important part, the timezone!! The problem with SimpleDateFormatis that it expect a TimeZone in +/-hhmmand mine is in +/-hh:mmformat.

几乎一切都很好,除了最重要的部分,时区SimpleDateFormat的问题在于它需要一个时区,+/-hhmm而我的+/-hh:mm格式是。

Also, and I don't know why, this works:

另外,我不知道为什么,这有效:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S Z");
Date date1 = sdf.parse("2011-06-07T14:08:59.697 -0700", new java.text.ParsePosition(0));

But this does not (the space before the timezone):

但这不是(时区之前的空间):

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SZ");
Date date1 = sdf.parse("2011-06-07T14:08:59.697-0700", new java.text.ParsePosition(0));

What is the correct format to transform this date 2011-06-07T14:08:59.697-07:00to a java date ?

将此日期2011-06-07T14:08:59.697-07:00转换为 java 日期的正确格式是什么?

Thanks for your help!

谢谢你的帮助!

回答by Jesper

That looks like the ISO 8601 standard date and time format as it is used in XML. Unfortunately, Java's SimpleDateFormatdoesn't support that format properly, because it can't deal with the colon in the timezone.

这看起来像 XML 中使用的 ISO 8601 标准日期和时间格式。不幸的是,JavaSimpleDateFormat不能正确支持该格式,因为它无法处理时区中的冒号。

However, the javax.xmlpackage contains classes that can deal with this format.

但是,该javax.xml包包含可以处理这种格式的类。

String text = "2011-06-07T14:08:59.697-07:00";
XMLGregorianCalendar cal = DatatypeFactory.newInstance().newXMLGregorianCalendar(text);

If you need it as a java.util.Calendarthen you can call toGregorianCalendar()on it:

如果你需要它,java.util.Calendar那么你可以调用toGregorianCalendar()它:

Calendar c2 = cal.toGregorianCalendar();

And ofcourse you can get a java.util.Dateout of that:

当然,你可以从中得到一个java.util.Date

Date date = c2.getTime();

You could also use the popular Joda Timelibrary which natively supports this format (and has a much better API for dealing with dates and times than Java's standard library).

您还可以使用流行的Joda Time库,它本机支持这种格式(并且具有比 Java 标准库更好的处理日期和时间的 API)。

回答by lepike

Java SimpleDateFormatdoesn't support colon in the timezone information. You should use other implementation such as JodaTime.

Java SimpleDateFormat不支持时区信息中的冒号。您应该使用其他实现,例如JodaTime

Example usage:

用法示例:

String dateString = "2011-06-07T14:08:59.697-07:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(dateString);
System.out.println(dateTime);

maven pom.xml dependency if needed:

如果需要,Maven pom.xml 依赖项:

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>1.6.2</version>
</dependency>

Hope it helps.

希望能帮助到你。

回答by Peter Lawrey

The problem with Sis that it will produce three digit milli-seconds but will not parse three digits.

问题S在于它会产生三位数的毫秒数,但不会解析三位数。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
System.out.println(sdf.parse("2011-06-07T14:08:59.697-0700"));

prints

印刷

Tue Jun 07 22:08:59 BST 2011

It appears you need to remove the :in the timezone.

看来您需要删除:时区中的 。

回答by Bohemian

It looks like DateFormat can't find the end of milliseconds and the start of timezone. Regex to the rescue!

看起来 DateFormat 找不到毫秒的结束和时区的开始。正则表达式来救援!

This is a work around:

这是一个解决方法:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S Z");
    Date date1 = sdf.parse("2011-06-07T14:08:59.697-0700".replaceAll("(?=.{5}$)", " "));

The regex puts a space in and this code executes without error.

正则表达式在其中放置一个空格,并且此代码执行时没有错误。