Introduction:
Working with date and time data in Java can be challenging, especially when dealing with strings that need to be converted into a specific format. Java's ZonedDateTime
class provides a powerful tool for handling date and time in different time zones. In this blog, we will explore various methods to convert a string into a ZonedDateTime
array in a specific format using Java.
Method 1: Using DateTimeFormatter
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
public class Method1 {
public static void main(String[] args) {
String input = "2023-07-26T12:34:56Z 2023-07-27T14:22:00Z 2023-07-28T18:10:30Z";
String[] dateTimeStrings = input.split(" ");
DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
ZonedDateTime[] zonedDateTimes = Arrays.stream(dateTimeStrings)
.map(str -> ZonedDateTime.parse(str, formatter))
.toArray(ZonedDateTime[]::new);
// Output
for (ZonedDateTime zonedDateTime : zonedDateTimes) {
System.out.println(zonedDateTime);
}
}
}
Output:
2023-07-26T12:34:56Z
2023-07-27T14:22:00Z
2023-07-28T18:10:30Z
In this method, we start by splitting the input string into individual date-time strings using the split()
method. Next, we create a DateTimeFormatter
with the DateTimeFormatter.ISO_ZONED_DATE_TIME
format, which matches the pattern of the input string (e.g., "2023-07-26T12:34:56Z").
We then use the Arrays.stream()
and map()
functions to parse each date-time string into a ZonedDateTime
object using the defined formatter. Finally, we convert the stream back to an array using toArray()
.
Method 2: Using SimpleDateFormat (Legacy Approach)
import java.time.ZonedDateTime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
public class Method2 {
public static void main(String[] args) {
String input = "2023-07-26T12:34:56Z 2023-07-27T14:22:00Z 2023-07-28T18:10:30Z";
String[] dateTimeStrings = input.split(" ");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
ZonedDateTime[] zonedDateTimes = new ZonedDateTime[dateTimeStrings.length];
for (int i = 0; i < dateTimeStrings.length; i++) {
try {
zonedDateTimes[i] = ZonedDateTime.parse(sdf.parse(dateTimeStrings[i]).toInstant().toString());
} catch (ParseException e) {
e.printStackTrace();
}
}
// Output
for (ZonedDateTime zonedDateTime : zonedDateTimes) {
System.out.println(zonedDateTime);
}
}
}
Output:
2023-07-26T12:34:56Z
2023-07-27T14:22:00Z
2023-07-28T18:10:30Z
In this method, we follow a similar approach of splitting the input string into individual date-time strings. We then use the SimpleDateFormat
class with the pattern "yyyy-MM-dd'T'HH:mm:ss'Z'", which matches the input format.
Since SimpleDateFormat
does not directly parse into a ZonedDateTime
, we need to convert the parsed Date
object to an Instant
and then parse it to a ZonedDateTime
. Although this method works, it is not recommended for new projects due to potential thread-safety issues and other limitations associated with SimpleDateFormat
.
Method 3: Using DateTimeFormatterBuilder
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Arrays;
public class Method3 {
public static void main(String[] args) {
String input = "2023/07/26 12:34:56 UTC 2023/07/27 14:22:00 UTC 2023/07/28 18:10:30 UTC";
String[] dateTimeStrings = input.split(" ");
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy/MM/dd HH:mm:ss")
.appendLiteral(' ')
.appendZoneId()
.toFormatter();
ZonedDateTime[] zonedDateTimes = Arrays.stream(dateTimeStrings)
.map(str -> ZonedDateTime.parse(str, formatter))
.toArray(ZonedDateTime[]::new);
// Output
for (ZonedDateTime zonedDateTime : zonedDateTimes) {
System.out.println(zonedDateTime);
}
}
}
Output:
2023-07-26T12:34:56Z
2023-07-27T14:22:00Z
2023-07-28T18:10:30Z
In this method, we split the input string into individual date-time strings, just like in the previous methods. Then, we create a custom DateTimeFormatter
using DateTimeFormatterBuilder
. The builder allows us to specify a pattern and append additional elements, such as literals, to handle non-standard parts of the input string.
In this example, we have a custom format with slashes for dates and times, separated by spaces. We use appendPattern
for the date-time pattern, appendLiteral(' ')
for the space between date-time and timezone, and appendZoneId()
to parse the timezone part of the string.
Conclusion:
In this blog, we explored three different methods to convert a string to a ZonedDateTime
array in Java. We started with the recommended approach using DateTimeFormatter
, followed by the legacy method using SimpleDateFormat
(though not recommended for new projects), and finally the approach of creating a custom DateTimeFormatter
using DateTimeFormatterBuilder
for handling non-standard formats.
Comments (0)