Issue
I am having a java program . In my java program the input will be date strings of different formats like the following
a) 2021-10-09T08:00:01.111Z ( with millisecond)
b) 2021-10-09T08:00:01Z ( without milliseconds)
c) 2021-10-09T08:00Z ( no seconds )
d) 2021-10-09T08Z ( no minutes )
Currently I am using a built in date formatter DateTimeFormatter.ISO_OFFSET_DATE_TIME . However when i run my sample snippet program it fails
the following is my sample program
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class Tester2 {
public static void main(String[] args) {
OffsetDateTime t1 = OffsetDateTime.parse("2021-10-09T08:00:01.111Z");;
System.out.println(t1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
OffsetDateTime t2 = OffsetDateTime.parse("2021-10-09T08:00:01Z");;
System.out.println(t2.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
OffsetDateTime t3 = OffsetDateTime.parse("2021-10-09T08:00Z");;
System.out.println(t3.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
OffsetDateTime t4 = OffsetDateTime.parse("2021-10-09T08Z");;
System.out.println(t4.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
}
}
However when I run my program it fails with the following output.
2021-10-09T08:00:01.111Z
2021-10-09T08:00:01Z
2021-10-09T08:00:00Z
Exception in thread "main" java.time.format.DateTimeParseException: Text '2021-10-09T08Z' could not be parsed at index 13
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:404)
at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:389)
at test.Tester2.main(Tester2.java:20)
The following is the output I want
2021-10-09T08:00:01.111Z
2021-10-09T08:00:01Z
2021-10-09T08:00:00Z
2021-10-09T08:00:00Z
Is there any java built in data formatter that will help me to achieve the desired output. If there is no such dateformatter could you help me to write a new date formatter ? Another solutions are welcome.
Solution
DataTimeFormatter
supports optional parsing patterns with [
and ]
characters.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH[:mm[:ss[.SSS]]]X");
OffsetDateTime t1 = OffsetDateTime.parse("2021-10-09T08:00:01.111Z", formatter);
System.out.println(t1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
OffsetDateTime t2 = OffsetDateTime.parse("2021-10-09T08:00:01Z", formatter);
System.out.println(t2.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
OffsetDateTime t3 = OffsetDateTime.parse("2021-10-09T08:00Z", formatter);
System.out.println(t3.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
OffsetDateTime t4 = OffsetDateTime.parse("2021-10-09T08Z", formatter);
System.out.println(t4.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
Output is what you asked for:
2021-10-09T08:00:01.111Z
2021-10-09T08:00:01Z
2021-10-09T08:00:00Z
2021-10-09T08:00:00Z
Answered By - BccHnw
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)