Parsing an ISO-8601 DateTime with a colon offset in Java

I am having problems processing the date in java, I have a weird date format. How to parse 2013-04-03T17:04:39.9430000+03:00 date in java for formatting dd.MM.yyyy HH:mm in java?

+5
source share
4 answers

The β€œstrange” format in question is ISO-8601 - it is very widely used. You can use SimpleDateFormat to reformat it in the most convenient way for you:

 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.

+17
source

TL; dr

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

ISO 8601

As others have noted, your format is not at all weird. This is actually a standard format. This format refers to a collection defined in the ISO 8601 format .

microseconds

These seven digits of the decimal fraction of a second, .9430000 , represent nanoseconds . Older date and time classes associated with the earliest versions of Java (java.util.Date/.Calendar/java.text.SimpleDateFormat) are only created for milliseconds (three decimal digits). Input values ​​like yours cannot be handled by old classes.

java.time

Fortunately, Java introduced new date and time classes that supplanted these old classes. New ones are in java.time . These new classes can handle nanoseconds (up to nine decimal digits), so there is no problem.

The java.time platform is built into Java 8 and later. Defined in JSR 310 . Most of the functionality was ported to Java 6 & 7 in the ThreeTen-Backport project and additionally adapted for Android in the ThreeTenABP project.

OffsetDateTime

OffsetDateTime represents a moment in the timeline with an offset from UTC . The input line 2013-04-03T17:04:39.9430000+03:00 has an offset of three hours ahead of UTC .

The java.time classes use ISO 8601 formats by default when parsing / generating strings. Therefore, there is no need to define a formatting pattern. We can parse this line directly.

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

Line generation

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

For a different format, define a formatting template.

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

Timezone

Please note that your entry has an offset from UTC, but not a true time zone. The time zone is the offset plus the rules for handling anomalies, such as daylight saving time (DST). For a real time zone, use ZoneId to get a ZonedDateTime . Look for many examples in Kara.


About java.time

The java.time framework is built into Java 8 and later. These classes supersede the problematic old obsolete date and time classes, such as java.util.Date , Calendar , & & SimpleDateFormat .

The Joda-Time project, which is now in maintenance mode, recommends switching to java.time classes.

To learn more, check out the Oracle Tutorial . And look in Kara for many examples and explanations. Specification: JSR 310 .

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

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a testing ground for possible future additions to java.time. Here you can find some useful classes, such as Interval , YearWeek , YearQuarter and even more .

+9
source

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

0
source

Please use this method to parse an 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 ); } 
0
source

All Articles