将ISO 8601日期转换为标准字符串格式 [英] Convert ISO 8601 Date to a Standard String Format

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

问题描述

我正在尝试将有效的 ISO 8601 字符串转换为一致的格式,以便进行排序并可以使用简单的字典顺序进行搜索.

I am trying to convert a valid ISO 8601 string to a consistent format so that sort and search using simple lexicographical order is possible.

我的应用程序可以使用以下任何格式接收日期/时间,例如:

My application could receive a date/time in any of the following formats, for example:

2015-02-05T02:05:17.000+00:00
2015-02-05T02:05:17+00:00
2015-02-05T02:05:17Z

这些都代表相同的日期/时间,我想将它们都转换为规范的形式进行存储,例如:

These all represent the same date/time and I would like to convert them all to a canonical form for storage, say:

2015-02-05T02:05:17.000Z

我的第一个想法是使用将符合ISO 8601的字符串转换为java.util.Date ,然后转换回所需的字符串,但这在处理较不精确的日期/时间时会崩溃,例如:

My first thought was to just parse them using a technique from Converting ISO 8601-compliant String to java.util.Date, and then convert back to the desired string, but this breaks down when dealing with less precise date/times, for example:

2015-02-05T02:05:17Z
2015-02-05T02:05Z
2015-02-05Z
2015-02Z
2015Z

应保留这些时间的不精确性.它们不应转换为:

The imprecision of these times should be preserved. They should not be converted to:

2015-02-05T00:00:00.000Z

我看过Java 8和Joda-Time,但他们似乎想将所有内容都视为特定的时间点,并且无法建模不精确的性质或部分日期/时间.

I've looked Java 8 and Joda-Time, but they seem to want to treat everything as specific points in time, and can't model the imprecise nature or partial dates/times.

更新:

使用Java 8,我可以做到:

Using Java 8, I can do:

OffsetDateTime dateTime = OffsetDateTime.parse("2015-02-05T02:05:17+00:00");
System.out.println(dateTime.toString());

这给了我

2015-02-05T02:05:17Z

这是我想要的,但是:

OffsetDateTime dateTime = OffsetDateTime.parse("2015-02-05T02:05:17.000+00:00");
System.out.println(dateTime.toString());

还给了我

2015-02-05T02:05:17Z

请注意,java放弃了毫秒精度.指定000与未指定任何内容一样,这似乎不太正确.

Notice that java has thrown away the millisecond precision. Specifying 000 is treated the same as not specifying anything, which doesn't seem quite right.

推荐答案

在Java 8中,您可以使用String上的/api/java/time/LocalDateTime.html#parse-java.lang.CharSequence-"rel =" noreferrer> LocalDateTime.parse()上,如果不提供模式,则为String格式为 ISO 8601 .

In Java 8, you can use LocalDate.parse() or LocalDateTime.parse() on a String without providing it with a pattern, if the String is in ISO 8601 format.

parse(), 从文本字符串(例如2007-12-03)获取LocalDate的实例. 该字符串必须表示一个有效日期,并使用 DateTimeFormatter.ISO_LOCAL_DATE.

parse(), Obtains an instance of LocalDate from a text string such as 2007-12-03. The string must represent a valid date and is parsed using DateTimeFormatter.ISO_LOCAL_DATE.

DateTimeFormatter.ISO_LOCAL_DATE, 这将返回一个能够格式化和解析的不可变格式化程序 ISO 8601

DateTimeFormatter.ISO_LOCAL_DATE, This returns an immutable formatter capable of formatting and parsing the ISO 8601

例如,

String strDate = "2015-08-04";
LocalDate aLD = LocalDate.parse(strDate);
System.out.println("Date: " + aLD);

String strDatewithTime = "2015-08-04T10:11:30";
LocalDateTime aLDT = LocalDateTime.parse(strDatewithTime);
System.out.println("Date with Time: " + aLDT);

给予

Date: 2015-08-04
Date with Time: 2015-08-04T10:11:30

更新:

您的日期,即"2015-02-05T02:05:17.000+00:00"仅在以下情况时抛出零 它们都是零,如果纳秒的值不是零,那么它将显示的很好,但是,如果您也想显示零,则可以简单地添加if/else块并将零添加到日期if上十亿分之一秒是零(yourdate.getNano()==0),else照原样打印,

your date, i.e "2015-02-05T02:05:17.000+00:00" would only throw zeros away when they are all zeros, if the value of nano seconds is other than zeros then it will display just fine, however, if you want to display zeros too then you can simply add if/else block and append zeros to your date if your nano-of-seconds are zeros (yourdate.getNano()==0), else print it as it is,

String dateTimestr = "2015-02-05T02:05:17.000+00:00";

OffsetDateTime dateTime = OffsetDateTime.parse(dateTimestr);

if ((dateTime.getNano() == 0) && (dateTimestr.length() > 25 ))
    System.out.println(dateTime.toLocalDateTime() + ".000Z");

else
    System.out.println(dateTime.toString());

会给出

2015-02-05T02:05:17.000Z

将日期更改为

String dateTimestr = "2015-02-05T02:05:17+00:00";

给予

2015-02-05T02:05:17Z

将日期更改为

String dateTimestr = "2015-02-05T02:05:17.100+00:00";

给予

2015-02-05T02:05:17.100Z

将其更改为

String dateTimestr = "2015-02-05T02:05:17Z";

给予

2015-02-05T02:05:17Z

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

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