java 用Java中的冒号偏移量解析ISO-8601 DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16336643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:41:05  来源:igfitidea点击:

Parsing ISO-8601 DateTime with offset with colon in Java

javadatetimedatetime-parsing

提问by vtokmak

I have a trouble with parsing date time in java, I have a strange date time format. How can I parse 2013-04-03T17:04:39.9430000+03:00date time in java to format dd.MM.yyyy HH:mmin java?

我在解析 java 中的日期时间时遇到了麻烦,我有一个奇怪的日期时间格式。如何2013-04-03T17:04:39.9430000+03:00在java中解析日期时间以在java中格式化dd.MM.yyyy HH:mm

回答by drew moore

The "strange" format in question is ISO-8601- its very widely used. You can use SimpleDateFormatto reformat it in most way you please:

有问题的“奇怪”格式是ISO-8601- 它的使用非常广泛。您可以使用SimpleDateFormat以大多数方式重新格式化它:

SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
DateTime dtIn = inFormat.parse(dateString});  //where dateString is a date in ISO-8601 format
SimpleDateFormat outFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
String dtOut = outFormat.format(dtIn);
//parse it into a DateTime object if you need to interact with it as such

will give you the format you mentioned.

会给你你提到的格式。

回答by Basil Bourque

tl;dr

tl;博士

OffsetDateTime.parse( "2013-04-03T17:04:39.9430000+03:00" ).format( DateTimeFormatter.ofPattern( "dd.MM.uuuu HH:mm" ) )

ISO 8601

ISO 8601

As the others noted, your format is not strange at all. Indeed it is a standard format. That format is one of a collection defined by the ISO 8601format.

正如其他人所指出的,您的格式一点也不奇怪。事实上,它是一种标准格式。该格式是ISO 8601格式定义的集合之一。

Microseconds

微秒

Those seven digits of a decimal fraction of a second, .9430000, represents nanoseconds. The old date-time classes bundled with the earliest versions of Java (java.util.Date/.Calendar/java.text.SimpleDateFormat) are built only for milliseconds(three digits of decimal fraction). Such input values as your cannot be handled by the old classes.

秒的十进制小数的那七位数字.9430000代表纳秒。与最早版本的 Java (java.util.Date/.Calendar/java.text.SimpleDateFormat) 捆绑的旧日期时间类仅针对毫秒(小数的三位数字)构建。旧类无法处理您这样的输入值。

java.time

时间

Fortunately Java now has newer date-time classes that supplant those old classes. The new ones are in the java.timeframework. These new classes can handle nanoseconds (up to nine digits of decimal fraction), so no problem there.

幸运的是,Java 现在有更新的日期时间类来取代那些旧的类。新的在java.time框架中。这些新类可以处理纳秒(最多九位小数),所以没问题。

The java.timeframework is built into Java 8and later. Defined in JSR 310. Much of the functionality is back-ported to Java 6 & 7 in the ThreeTen-Backportproject, and further adapted for Android in the ThreeTenABPproject.

java.time框架内置的Java 8和更高版本。在JSR 310 中定义。大部分功能在ThreeTen-Backport项目中向后移植到 Java 6 和 7 ,并在ThreeTenABP项目中进一步适用于 Android 。

OffsetDateTime

OffsetDateTime

An OffsetDateTimerepresents a moment on the timeline with an offset-from-UTC. Your input string, 2013-04-03T17:04:39.9430000+03:00, has an offset that is three hours ahead of UTC.

AnOffsetDateTime表示时间轴上的一个时刻,偏移量为 UTC。您的输入字符串2013-04-03T17:04:39.9430000+03:00具有比UTC早三个小时的偏移量。

The java.time classes use ISO 8601 formats by default when parsing/generating strings. So no need to define a formatting pattern. We can directly parse that string.

java.time 类在解析/生成字符串时默认使用 ISO 8601 格式。所以不需要定义格式模式。我们可以直接解析那个字符串。

OffsetDateTime odt = OffsetDateTime.parse( "2013-04-03T17:04:39.9430000+03:00" );

Generating Strings

生成字符串

To generate a string representation in the same style, call its toStringmethod.

要生成相同样式的字符串表示,请调用其toString方法。

For a different format define a formatting pattern.

对于不同的格式,定义格式模式。

DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd.MM.uuuu HH:mm" );
String output = odt.format( formatter );

Time Zone

时区

Note that your input has an offset-from-UTC but not a true time zone. A time zone is an offset plusrules for handling anomalies such as Daylight Saving Time (DST). For a true time zone use ZoneIdto get a ZonedDateTime. Search Stack Overflow for many examples.

请注意,您的输入具有与 UTC 的偏移量,但不是真正的时区。时区是用于处理夏令时 (DST) 等异常情况的偏移量规则。对于真正的时区,使用ZoneId获取ZonedDateTime. 在 Stack Overflow 上搜索许多示例。



About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

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

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

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

You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*classes.

您可以直接与数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject 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.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多

回答by luksch

For serious work with date and time in java I would suggest using a better implementation than the Calendarstuff. I would use Joda, and therein you can use DateTimeFormatter

对于在 java 中处理日期和时间的严肃工作,我建议使用比日历更好的实现。我会使用Joda,其中你可以使用DateTimeFormatter

回答by valerybodak

Please use this method for parse ISO8601 Date without any library. http://www.java2s.com/Code/Java/Data-Type/ISO8601dateparsingutility.htm

请使用此方法在没有任何库的情况下解析 ISO8601 日期。 http://www.java2s.com/Code/Java/Data-Type/ISO8601dateparsingutility.htm

public static Date parseISO8601Date(String input ) throws java.text.ParseException {

    //NOTE: SimpleDateFormat uses GMT[-+]hh:mm for the TZ which breaks
    //things a bit.  Before we go on we have to repair this.
    SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz" );

    //this is zero time so we need to add that TZ indicator for
    if ( input.endsWith( "Z" ) ) {
        input = input.substring( 0, input.length() - 1) + "GMT-00:00";
    } else {
        int inset = 6;

        String s0 = input.substring( 0, input.length() - inset );
        String s1 = input.substring( input.length() - inset, input.length() );

        input = s0 + "GMT" + s1;
    }

    return df.parse( input );

}