将时间字符串转换为ISO 8601格式 [英] Converting a time String to ISO 8601 format

查看:233
本文介绍了将时间字符串转换为ISO 8601格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以 2015-08-20T08:26:21.000Z 格式创建字符串


2015-08-20T08:26: 21Z

I am trying to create a String in a format like 2015-08-20T08:26:21.000Z
to 2015-08-20T08:26:21Z

我知道可以使用一些字符串拆分技术来完成,但我想知道是否有一个优雅的解决方案(最小的代码更改) 。

I know it can be done with some String splitting techniques, but i am wondering if there is an elegant solution for that (with minimal code changes).

上述两个都是时间字符串,我需要的最后一个是ISO 8601中的日期。 http://tools.ietf.org/html/rfc3339#section-5.6

Both of the above are time strings, the final one which i need is Date in ISO 8601 . http://tools.ietf.org/html/rfc3339#section-5.6

我尝试了一些类似的问题,例如在java中将日期字符串转换为毫秒但它们实际上并没有解决目的。

I have tried a few similar questions like converting a date string into milliseconds in java but they dont actually solve the purpose.

还尝试使用:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
String nowAsString = df.format(new Date());

但它仍然不执行任何String to String转换。得到以下错误:

But it still does not do any String to String conversions. Getting the following error:

23:04:13,829 WARN [RuntimeExceptionMapper]捕获RuntimeException:{}:java.lang.IllegalArgumentException:无法将给定的Object格式化为日期

是否有人可以推荐一些图书馆?

Is there some library which someone can suggest ?

谢谢。

推荐答案

tl; dr



tl;dr

Instant.parse( "2015-08-20T08:26:21.000Z" )
       .toString()




2015-08-20T08:26:21Z

2015-08-20T08:26:21Z



日期时间格式化程序



如果您只想消除 .000 ,那么使用日期时间对象来解析输入字符串value,然后以不同的格式生成该日期时间值的新字符串表示。

Date-Time Formatter

If all you want to do is eliminate the .000, then use date-time objects to parse your input string value, then generate a new string representation of that date-time value in a different format.

顺便说一下,如果这是你的目标,那么问题的标题就没有意义,因为第一句中提到的两个字符串都是有效的 ISO 8601 格式化字符串。

By the way, if that is your goal, the Question’s title make no sense as both strings mentioned in the first sentence are valid ISO 8601 formatted strings.


  • 2015-08-20T08:26:21.000Z

  • 2015-08-20T08:26:21Z

  • 2015-08-20T08:26:21.000Z
  • 2015-08-20T08:26:21Z

Java 8及更高版本有新的 java.time包。这些新类取代了旧的java.util.Date/.Calendar& java.text.SimpleDateFormat类。那些旧课程令人困惑,麻烦和有缺陷。

Java 8 and later has the new java.time package. These new classes supplant the old java.util.Date/.Calendar & java.text.SimpleDateFormat classes. Those old classes were confusing, troublesome, and flawed.

如果你想要的只是UTC时区,那么你可以使用 Instant 类。此类表示时间轴上的一个点,不考虑任何特定时区(基本上是UTC)。

If all you want is UTC time zone, then you can use the Instant class. This class represents a point along the timeline without regard to any particular time zone (basically UTC).

调用Instant的 toString 使用 DateTimeFormatter.ISO_INSTANT 格式化程序实例。此格式化程序自动灵活地关于小数秒。如果值有一整秒,则不会生成小数位(显然是问题想要的)。对于小数秒,数字显示在3,6或9的组中,根据需要表示高达纳秒分辨率的值。注意:此格式可能超过ISO 8601毫秒限制(3位小数)。

Calling an Instant’s toString generates a String representation of the date-time value using a DateTimeFormatter.ISO_INSTANT formatter instance. This formatter is automatically flexible about the fractional second. If the value has a whole second, no decimal places are generated (apparently what the Question wants). For a fractional second, digits appear in groups of 3, 6, or 9, as needed to represent the value up to nanosecond resolution. Note: this format may exceed ISO 8601 limit of milliseconds (3 decimal places).

此处是Java 8 Update 51中的一些示例代码。

Here is some example code in Java 8 Update 51.

String output = Instant.parse( "2015-08-20T08:26:21.000Z" ).toString( );
System.out.println("output: " + output );




输出:2015-08-20T08:26:21Z

output: 2015-08-20T08:26:21Z

更改为小数秒, .08

String output = Instant.parse( "2015-08-20T08:26:21.08Z" ).toString( );




输出:2015-08-20T08:26:21.080Z

output: 2015-08-20T08:26:21.080Z

如果对UTC以外的任何时区感兴趣,请创建 ZonedDateTime 来自 Instant <的对象/ code>。

If interested in any time zone other than UTC, then make a ZonedDateTime object from that Instant.

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

这篇关于将时间字符串转换为ISO 8601格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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