Receiving ParseException with String containing ISO 8601 Formatted Date using Java 1.6

我怕爱的太早我们不能终老 提交于 2019-12-13 00:47:21

问题


Keep getting a ParseException when I try to parse a ISO 8601 formatted String into a Java Date type.

String myDateString = "2014-07-04T22:59:36Z";  
TimeZone timeZone = TimeZone.getTimeZone("UTC");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'", Locale.US);
dateFormat.setTimeZone(timeZone);
Date formattedDate = dateFormat.parse(myDateString);

Keeps returning a ParseException:

Unparseable date: "2014-07-04T22:59:36Z"

What am I possibly doing wrong?


回答1:


You are missing the seconds in your date format:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);



回答2:


tl;dr

For Java 6 & 7, add the ThreeTen-Backport library to your project. For Java 8 and later, java.time is built-in.

Instant.parse( "2014-07-04T22:59:36Z" )

java.time

The modern way is with the java.time classes that supplant the troublesome old legacy date-time classes such as SimpleDateFormat, Date, and Calendar.

While built into Java 8 and later, a back-port to Java 6 and Java 7 is available. (see below)

Instant

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

The java.time classes use ISO 8601 formats by default. So no need to specify a formatting pattern. The Instant class can directly parse your input string.

Instant instant = Instant.parse( "2014-07-04T22:59:36Z" );

To generate a String in standard ISO 8601 format, simply call toString.

String output = instant.toString();

2014-07-04T22:59:36Z


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.



来源:https://stackoverflow.com/questions/25538918/receiving-parseexception-with-string-containing-iso-8601-formatted-date-using-ja

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!