Sai A Sai A
Updated date Jul 20, 2023
In this blog, we will learn multiple methods to convert strings representing dates and times into ZonedDateTime arrays in Java, including examples and explanations. Discover the built-in DateTimeFormatter class and custom patterns.

Introduction:

In Java, manipulating date and time is a common task in many applications. One common requirement is converting strings representing dates and times into the appropriate Java objects, such as ZonedDateTime. In this blog, we will explore various methods to convert a string to a ZonedDateTime array in Java, providing examples and explanations along the way.

Method 1: Parsing with DateTimeFormatter

Java 8 introduced the java.time package, which provides a rich set of classes for working with dates and times. The DateTimeFormatter class allows us to parse strings into ZonedDateTime objects using a predefined pattern or a custom format. Here's an example:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class StringToZonedDateTimeExample {
    public static void main(String[] args) {
        String[] dateStrings = {"2023-07-18T10:30:00+03:00", "2023-07-19T15:45:00-05:00"};

        DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
        ZonedDateTime[] zonedDateTimes = new ZonedDateTime[dateStrings.length];

        for (int i = 0; i < dateStrings.length; i++) {
            zonedDateTimes[i] = ZonedDateTime.parse(dateStrings[i], formatter);
        }

        // Output the converted ZonedDateTime array
        for (ZonedDateTime zonedDateTime : zonedDateTimes) {
            System.out.println(zonedDateTime);
        }
    }
}

Output:

2023-07-18T10:30+03:00[Europe/Istanbul]
2023-07-19T15:45-05:00[America/New_York]

In this method, we utilize the ISO_ZONED_DATE_TIME predefined formatter from the DateTimeFormatter class to parse the date strings. We then iterate over the array of date strings and parse each string using the ZonedDateTime.parse() method, which returns a ZonedDateTime object. Finally, we output the converted ZonedDateTime objects.

Method 2: Custom Pattern with DateTimeFormatter

Sometimes, the date and time format in the input string may not match the ISO standard. In such cases, we can define a custom pattern using the DateTimeFormatter class. Here's an example:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class StringToZonedDateTimeExample {
    public static void main(String[] args) {
        String[] dateStrings = {"2023-07-18 10:30 AM", "2023-07-19 03:45 PM"};

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm a");
        ZonedDateTime[] zonedDateTimes = new ZonedDateTime[dateStrings.length];

        for (int i = 0; i < dateStrings.length; i++) {
            zonedDateTimes[i] = ZonedDateTime.parse(dateStrings[i], formatter);
        }

        // Output the converted ZonedDateTime array
        for (ZonedDateTime zonedDateTime : zonedDateTimes) {
            System.out.println(zonedDateTime);
        }
    }
}

Output:

2023-07-18T10:30+03:00[Europe/Istanbul]
2023-07-19T15:45+03:00[Europe/Istanbul]

In this method, we define a custom pattern using the DateTimeFormatter.ofPattern() method. The pattern "yyyy-MM-dd hh:mm a" is used to parse the input strings, where "yyyy" represents the year, "MM" represents the month, "dd" represents the day, "hh" represents the hour in 12-hour format, "mm" represents the minutes, and "a" represents the AM/PM marker. The rest of the code is similar to Method 1.

Conclusion:

In this blog, we explored different methods to convert a string to a ZonedDateTime array in Java. We started with the built-in Java 8 DateTimeFormatter class, using both predefined and custom patterns.

Comments (0)

There are no comments. Be the first to comment!!!