Converting ISO 8601-compliant String to java.util.Date

Converting ISO 8601-compliant String to java.util.Date

When using dates and times across different time zones or sharing data between systems, it is important to use a standard format. In Java, converting an ISO 8601 string to a java.util.Date can be complex because the built-in APIs can be confusing.

In this blog, we will discuss the methods used for converting the ISO 8601-compliant String to the java.util.Date format.

Table of Contents:

What is ISO 8601 compliant?

ISO 8601 compliant refers to the standard way of representing the date and time in a consistent format. ISO 8601 is an international standard used for representing dates and times by the International Organization for Standardization (ISO). It ensures that the dates and times are represented in a format that avoids problems due to the different regional areas in the date formats, i.e., MM/DD/YYYY vs. DD/MM/YYYY.

Some of the key components of ISO 8601 are:

1. Date: The date is represented in the format of YYYY-MM-DD.

Example: 2025-03-21 (March 21, 2025).

2. Time: The time is represented in the format of HH: mm: ss.

Example: 14:35:00 (14 hours, 35 minutes, and 00 seconds).

3. Combined Date and Time: The date and time are combined using the letter “T” in between them.

Example: 2025-03-21T14:35:00 (March 21, 2025, 14:35:00).

4. Time Zone:  For UTC (Coordinated Universal Time), the letter “Z” is used to indicate zero offsets from UTC, i.e., Z stands for “Zulu” time, which is the same as UTC.

Example: 2025-03-21T14:35:00Z (March 21, 2025, 14:35:00 UTC).

Important Features of ISO 8601:

  • It provides a standard format of dates and times across all the cultures.
  • It ensures that systems (databases, APIs, etc.) from different regions can work together without issues related to date formatting.
  • The format YYYY-MM-DD makes sorting of dates easy.

Example of ISO 8601-compliant:

2025-03-21T14:35:00Z – Date and time in UTC (March 21, 2025, at 14:35:00 UTC).

Different Methods to Convert an ISO 8601 String into the java.util.Date

There are mainly 4 methods to convert the ISO 8601 string into the java.util.Date. These are as follows : 

Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
quiz-icon

1. Using SimpleDateFormat  

In Java 7 and earlier, the SimpleDateFormat class was used to convert the date and time strings into java.util.Date objects. This class offers a way to define a specific date-time pattern and change the strings into the Date objects.

Example:

Java

Output:

Using SimpleDateFormat 

Explanation: In the above code, an ISO 8601 string “2025-03-21T14:35:00Z” is changed into a Date object by using the SimpleDateFormat. The format and time zone are set to UTC, and then the string is parsed.

2. Using DateTimeFormatter  

Java introduced a modern and flexible way to manage date-time parsing and formatting. The DateTimeFormatter class is designed in such a way to handle the ISO 8601-compliant strings. It is the part of the new date-time API that is used more than the older SimpleDateFormat for parsing and formatting the dates.

DateTimeFormatter makes parsing ISO 8601 strings easy by automatically managing the different date and time formats, including the seconds, time zone, and the ‘Z’ for UTC.

Example:

Java

Output:

Using DateTimeFormatter

Explanation: In the above code DateTimeFormatter.ISO_INSTANT is used to convert an ISO 8601 string  “2025-03-21T14:35:00Z” into an Instant, which shows a certain time at that moment. It then changes the Instant into a java.util.Date object and then prints it. 

3. Using ZonedDateTime  

The ZonedDateTime class allows you to handle date and time with specific time zones, hence making it more useful for parsing ISO 8601-compliant strings that include time zone information, e.g., +02:00 or -05:00. 

Example:

Java

Output:

Using ZonedDateTime

Explanation: The above code parses an ISO 8601 string with a time zone “2025-03-21T14:35:00+02:00” into a ZonedDateTime using DateTimeFormatter, then changes ZonedDateTime into a java.util.Date. 

4. Using Instant 

For UTC dates, Instant is the simplest and most efficient method to handle the UTC dates. It is used for parsing the ISO 8601 strings that use the “Z” suffix. The Instant.parse() method directly changes the strings into an Instant object and then changes it into a java.util.Date.

Example:

Java

Output:

Using Instant 

Explanation: The above code uses Instant.parse() method to convert an ISO 8601 UTC string  “2025-03-21T14:35:00Z”  into an Instant. After that, it changes the Instant into a Date and prints it. This method is simple and efficient for working with UTC dates.

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

There are many ways to convert an ISO 8601-formatted string into a java.util.Date in Java, each of which is used for different purposes. The SimpleDateFormat method is used for older Java versions, while DateTimeFormatter and ZonedDateTime are more flexible for modern applications, especially when having time zone information. For UTC dates, Instant is the simplest and most efficient solution. Selecting the right method depends on the Java version, the need for a time zone, and the complexity of the date string.

If you want to learn more about Java, you can refer to our Java Course.

How to Convert ISO 8601 Compliant String to java.util.Date – FAQs

Q1. How to convert ISO 8601 string to instant in Java?

A string that follows the ISO-8601 standard for representing date and time can be easily converted to Instant via the Instant.parse() method.

Q2. What is the date format for ISO 8601 in Java?

The ISO 8601 format used is yyyy-MM-dd’T’HH:mm:ssZZ. This format uses the default TimeZone.

Q3. What is the default format of Java Util date?

It represents the date and time with a default format of yyyy-MM-dd-HH-mm-ss.

Q4. How to parse any date format in Java?

We can parse a string to a date instance using the parse() method of the SimpleDateFormat class.

Q5. What are t and z in the timestamp? What is the Z in ISO 8601 date?

The T is just a literal to separate the date from the time, and the Z means “zero hour offset,” also known as “Zulu time”.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner