Home  >  Article  >  Java  >  How to Parse ISO-8601 Dates with Colon Offsets in Java?

How to Parse ISO-8601 Dates with Colon Offsets in Java?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-30 18:47:02856browse

How to Parse ISO-8601 Dates with Colon Offsets in Java?

Parsing ISO-8601 Date with Colon Offset in Java

When dealing with date and time parsing in Java, you may encounter the ISO-8601 standard, widely used for representing dates and times. One specific format within ISO-8601 includes an offset with a colon, such as 2013-04-03T17:04:39.9430000 03:00.

Question: How can this date time format be parsed and converted to a desired format, such as dd.MM.yyyy HH:mm, in Java?

Answer:

The key to parsing ISO-8601 date times with colon offsets lies in using the SimpleDateFormat class from the Java java.text package. This class provides methods for parsing and formatting dates and times according to different patterns.

To parse the given ISO-8601 date time format:

<code class="java">SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
DateTime dtIn = inFormat.parse(dateString); // Assumes 'dateString' contains the ISO-8601 date</code>

Once parsed into a DateTime object, you can use another SimpleDateFormat to convert it to the desired format:

<code class="java">SimpleDateFormat outFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
String dtOut = outFormat.format(dtIn);</code>

The resulting dtOut variable will contain the date in the specified format, such as 03.04.2013 17:04. This approach allows for flexible and efficient parsing and formatting of ISO-8601 date times in Java.

The above is the detailed content of How to Parse ISO-8601 Dates with Colon Offsets in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn