Reputation: 1399
I have this code:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern(DateType.DATE_FORMAT)
.appendOffset("+HH:mm", "+00:00")
.toFormatter();
ZonedDateTime.parse("2018-06-09T14:09:30.020+03:00[Europe/Tallinn]", formatter);
DateType.java
:
public class DateType {
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_TIME_ZONE_FORMAT = DATE_FORMAT + "XXX";
}
But, I run this code, I have error:
java.time.format.DateTimeParseException: Text '2018-06-09T14:09:30.020+03:00[Europe/Tallinn]' could not be parsed at index 10
As I understand it, this is because of the formatter. What could be wrong?
Upvotes: 0
Views: 543
Reputation: 2490
What could be wrong?
I'd say, whatever you find at index 10, as the error message says. The 'T' is what's wrong. Because you never mentioned one in your DATE_FORMAT, you have a space instead.
After this problem, you would have the problem that your String contains a time zone name in addition to the offset, which your formatter was not informed about.
As the other answer says, these problems can be fixed by just relying on default parsers, as they understand this format.
Upvotes: 2
Reputation: 20891
I wonder why you didn't try default parser. I think it works as expected.
String dateTime = "2018-06-09T14:09:30.020+03:00[Europe/Tallinn]";
ZonedDateTime z = ZonedDateTime.parse(dateTime);
System.out.println(z);
Upvotes: 3