使用Java 1.6接收包含ISO 8601格式化日期的字符串的ParseException [英] Receiving ParseException with String containing ISO 8601 Formatted Date using Java 1.6

查看:124
本文介绍了使用Java 1.6接收包含ISO 8601格式化日期的字符串的ParseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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);
日期formattedDate = dateFormat.parse(myDateString);

继续返回ParseException:

 不可稀释的日期:2014-07-04T22:59:36Z

我可能做错了什么?

解决方案

tl; dr



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



java.time



现代的方式是使用java.time类替代麻烦的旧旧日期时间类,例如 SimpleDateFormat 日期日历



内置于Java 8及更高版本中,可以使用Java 6和Java 7的后端口。 (见下文)



即时



a href =http://docs.oracle.com/javase/8/docs/api/java/time/Instant.html =nofollow noreferrer> 即时 类代表 UTC 的时间轴上的一小时,分辨率为 nanoseconds (最多九(9)位小数)。



默认情况下,java.time类使用ISO 8601格式。所以不需要指定格式化模式 Instant 类可以直接解析输入字符串。

 即时即时= Instant.parse(2014-07-04T22:59:36Z);要生成标准ISO 8601格式的字符串,只需调用 toString ,



  String output = instant.toString(); 




2014-07-04T22:59:36Z







关于java.time



一个href =http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html =nofollow noreferrer> java.time 框架内置Java 8及更高版本。这些课程取代了麻烦的旧旧版日期时间课程,例如 java.util.Date 日历 ,& SimpleDateFormat



Joda-Time 项目,现在在维护模式中建议迁移到 java.time 类。



要了解更多信息,请参阅 Oracle教程。并搜索Stack Overflow的许多示例和解释。规格为 JSR 310



哪里可以获取java.time类?





ThreeTen-Extra 项目扩展了java.time和其他类。这个项目是未来可能添加到java.time的证明。您可能会在这里找到一些有用的课程,例如 Interval YearWeek YearQuarter 更多


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?

解决方案

tl;dr

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?

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.

这篇关于使用Java 1.6接收包含ISO 8601格式化日期的字符串的ParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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