Parsing ISO-8601 Date Time with Offset: Colon in Java
Parsing date time strings in Java can be challenging, especially when dealing with unfamiliar formats. This article addresses the issue of parsing an ISO-8601 date time string with an offset containing a colon, such as "2013-04-03T17:04:39.9430000 03:00."
Solution
ISO-8601 is a widely used standard for representing date and time information. To parse ISO-8601 date time strings in Java, you can use the SimpleDateFormat class. Here's a code snippet demonstrating how to parse the provided string and reformat it to the desired "dd.MM.yyyy HH:mm" format:
<code class="java">import java.text.SimpleDateFormat; import java.util.Date; public class ISO8601DateTimeParser { public static void main(String[] args) throws Exception { // Parse the ISO-8601 date time string SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); Date dtIn = inFormat.parse("2013-04-03T17:04:39.9430000+03:00"); // Reformat the date time string SimpleDateFormat outFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm"); String dtOut = outFormat.format(dtIn); // Print the reformatted date time string System.out.println(dtOut); // Output: 03.04.2013 17:04 } }</code>
This code will parse the given ISO-8601 date time string and reformat it to the specified "dd.MM.yyyy HH:mm" format.
The above is the detailed content of How to Parse ISO-8601 Date Time Strings with Offset Colon in Java?. For more information, please follow other related articles on the PHP Chinese website!