Parsing ISO-8601 DateTime with offset with colon in Java
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?
Solution 1:
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.
Solution 2:
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?
-
Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
-
Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
-
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.