Home > Java > javaTutorial > body text

How to Parse ISO-8601 Date Time Strings with Offset Colon in Java?

Mary-Kate Olsen
Release: 2024-10-31 07:58:01
Original
825 people have browsed it

How to Parse ISO-8601 Date Time Strings with Offset Colon in Java?

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template