Java不可稀释的日期 [英] Java unparseable date

查看:155
本文介绍了Java不可稀释的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下代码:

Date st = new SimpleDateFormat("HH:mm").parse(date);

我得到以下异常Unparseable date:2000-01-01T01:00:00Z (在偏移量4)。我该怎么解决?提前致谢。

And I get the following exception "Unparseable date: "2000-01-01T01:00:00Z" (at offset 4)". How can I fix it? Thanks in advance.

推荐答案

解析模式错误



您定义了一个格式化模式您希望输入字符串为小时和分钟。但是您的输入字符串有更多,多年,几个月等等。

Wrong Parsing Pattern

You defined a formatting pattern that says you expect the input string to be hours and minutes. But your input string has much more, years, months, and so on.

As的Java 8和更高版本,旧的java.util.Date/.Calendar和java.text.SimpleDateFormat已被新的 java.time包。尽可能避免使用旧类,因为它们被证明是令人困惑,麻烦和有缺陷的。

As of Java 8 and later, the old java.util.Date/.Calendar and java.text.SimpleDateFormat have been supplanted by the new java.time package. Avoid the old classes whenever possible as they have proven to be confusing, troublesome, and flawed.

您的输入字符串正在使用标准日期时间格式之一, a href =https://en.wikipedia.org/wiki/ISO_8601 =nofollow> ISO 8601 。幸运的是,java.time默认使用该格式。

Your input string is using one of the standard date-time formats defined by ISO 8601. Fortunately, java.time uses that format by default.

String input = "2000-01-01T01:00:00Z" ;
Instant instant = Instant.parse( input ) ;

An Instant 是时间轴上的一个时刻,基本上是在 UTC 。您可以将值调整为特定时区( ZoneId ),生成 ZonedDateTime 对象。

An Instant is a moment on the timeline basically in UTC. You may adjust the value to a particular time zone (a ZoneId), producing a ZonedDateTime object.

ZonedId zoneId = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.of( instant , zoneId ) ;

如果您在ZonedDateTime上调用 toString 你会得到一个字符串:

If you call toString on a ZonedDateTime, you will get a string like:


1999-12-31T20:00:00-05:00 [America / Montreal]

1999-12-31T20:00:00-05:00[America/Montreal]

ZonedDateTime类通过在括号中附加时区名称来扩展ISO 8601格式。

The ZonedDateTime class extends the ISO 8601 format by appending the name of the time zone in brackets.

这篇关于Java不可稀释的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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