Parsing ISO-8601 DateTime in java

2019-01-02 16:17发布

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:00 date time in java to format dd.MM.yyyy HH:mm in java?

4条回答
孤独总比滥情好
2楼-- · 2019-01-02 16:41

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

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.

查看更多
爱死公子算了
3楼-- · 2019-01-02 16:44

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

查看更多
旧时光的记忆
4楼-- · 2019-01-02 16:52

tl;dr

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

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 8601 format.

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.

java.time

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

The java.time framework is built into Java 8 and later. Defined in JSR 310. Much of the functionality is back-ported to Java 6 & 7 in the ThreeTen-Backport project, and further adapted for Android in the ThreeTenABP project.

OffsetDateTime

An OffsetDateTime represents 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.

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.

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 toString method.

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 plus rules for handling anomalies such as Daylight Saving Time (DST). For a true time zone use ZoneId to get a ZonedDateTime. Search Stack Overflow for many examples.


About java.time

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

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

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

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

Where to obtain the java.time classes?

The ThreeTen-Extra project 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.

查看更多
无色无味的生活
5楼-- · 2019-01-02 16:55

Please use this method for parse ISO8601 Date without any library. 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 );

}
查看更多
登录 后发表回答