SimpleDateFormat的添加一些分钟 [英] SimpleDateFormat add some minutes

查看:529
本文介绍了SimpleDateFormat的添加一些分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着从一个JSONObject的分析日期

Im trying parse a date from a JSONObject

"timcre_not":"2013-12-11 21:25:04.800842+01"

和我解析与

mDate = new SimpleDateFormat("y-M-d h:m:s.SSSSSSZZ",  
                          Locale.ENGLISH).parse(json.getString("timcre_not"));

但mDate值:

but the mDate value is:

Wed Dec 11 21:38:24 CET 2013

这是怎么回事?

What is happening?

推荐答案

由treeno 的答案是正确的。

The answer by treeno is correct.

作为一种替代方法,您可以使用第三方的开源乔达时 。乔达时经常被用来取代与java.util.Date和放大器;在Java中(和Android)。发现Calendar类

As an alternative, you can use the third-party open-source Joda-Time. Joda-Time is often used to supplant the java.util.Date & Calendar classes found in Java (and Android).

您的字符串是松散 ISO 8601 格式。替换空间与拉丁大写字母TT字得到了严格的ISO 8601格式。

The string you have is loosely in ISO 8601 format. Replace that SPACE with a LATIN CAPITAL LETTER T "T" character to get a strict ISO 8601 format.

乔达时的 日期时间 类接受了ISO 8601字符串直接给它的构造。一个陷阱:与java.util.Date,一个DateTime只追踪到毫秒不微秒。但在乔达时,而不是抛出一个错误,日期时间只是截断(忽略)额外的(超出3)位小数。

Joda-Time's DateTime class accepts an ISO 8601 string directly to its constructor. One catch: As with java.util.Date, a DateTime tracks only to the millisecond not microsecond. But in Joda-Time, rather than throw an error, the DateTime merely truncates (ignores) the extra (beyond 3) decimal places.

下面是一个使用乔达时2.3和Java 8的一些例子code。

Here is some example code using Joda-Time 2.3 and Java 8.

String input = "2013-12-11 21:25:04.800842+01";
String string = input.replace( " ", "T" ); // Replace SPACE with "T" for strict ISO 8601 format.

DateTime dateTimeUtc = new DateTime( string, DateTimeZone.UTC );

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
DateTime dateTimeParis = new DateTime( string, timeZone );

转储到控制台...

Dump to console…

System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeParis: " + dateTimeParis );

在运行...

dateTimeUtc: 2013-12-11T20:25:04.800Z
dateTimeParis: 2013-12-11T21:25:04.800+01:00

爪哇8

我试图用新的java.time。* Java中的类8解析你的字符串。

Java 8

I tried using the new java.time.* classes in Java 8 to parse your string.

ZonedDateTime zonedDateTime = ZonedDateTime.parse( string );

不幸的是,解析器不容忍的时区偏移量是缩短 +01 。我尝试了更长的 +01:00 和它的工作。这似乎是一个缺陷,在Java实现,不是你的字符串。缩短的偏移是允许在ISO 8601。虽然两者我和RFC 3339使用preFER(ISO 8601的近轮廓)长 +01:00 中, ISO标准母鹿允许它,所以应该在java.time。*类。我提出错误编号:9009717与Oracle

Unfortunately, the parser did not tolerate the time zone offset being the shortened +01. I tried the longer +01:00 and it worked. This seems to be a flaw in the Java implementation, not your string. The shortened offset is allowed in ISO 8601. While both I and RFC 3339 (a near-profile of ISO 8601) prefer using the longer +01:00, the ISO standard doe allow it and so should the java.time.* classes. I filed Bug Id: 9009717 with Oracle.

如果可能的话,建议您日期他们所使用的更严格的和共同的ISO 8601格式包括源:

If possible, suggest to the source of your date that they use the more strict and common ISO 8601 format including:

  • 使用字母T来代替空间的
  • 在较长的时区偏移量,+01:00,而不是+01

这篇关于SimpleDateFormat的添加一些分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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