7

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
vtokmak
  • 1,496
  • 6
  • 35
  • 66
  • 3
    You *parse* from text to non-text. You *format* from non-text to text. It sounds like you need two `SimpleDateFormat` objects - one to parse, and the other to format. Try this, and edit your question with your attempt if you don't succeed. – Jon Skeet May 02 '13 at 11:15
  • Dup: http://stackoverflow.com/q/7604237/642706 – Basil Bourque Aug 21 '16 at 21:38
  • 2
    FYI, that input string is in standard [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. – Basil Bourque Sep 02 '16 at 20:19
  • @BasilBourque thank you for your response, "drewmoore" also stated below which format that date is. – vtokmak Sep 02 '16 at 22:04

4 Answers4

17

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.

drew moore
  • 31,565
  • 17
  • 75
  • 112
  • It's strange that (Simple)DateFormat doesn't come with a standard option for the widely-used ISO-8601 format, instead requiring developers to roll their own format string. Maybe it's because ISO-8601 can have so many different variations. – LarsH Sep 26 '17 at 22:16
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 23 '18 at 20:14
  • 2
    This Answer fails to address the microseconds/nanoseconds in the Question. – Basil Bourque May 02 '19 at 00:43
11

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

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

luksch
  • 11,497
  • 6
  • 38
  • 53
  • Is my answer so bad to make a downvote? any comment from the downvoter on that? Maybe I can improve. I really think that Joda time has many advantages over the standard java date/time implementation. – luksch May 02 '13 at 12:23
  • I wasn't the downvoter, and I love JodaTime and use it frequently, but it isn't necessary for what the OP wanted to do, and thus wasn't really a relevant answer to the question. – drew moore May 02 '13 at 14:05
  • You did not really answer OPs question. He could be in a situation where it is not possible for him to add an external library to his project. I am not the downvoter, this is just my take on it. Your answer is still correct though, just not 100% an answer to OP. – A. Steenbergen Jan 23 '14 at 18:07
  • FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [*java.time*](http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 23 '18 at 20:14
0

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 );

}
valerybodak
  • 4,195
  • 2
  • 42
  • 53