Reputation: 5659
I have the following unit test:
@Test
public void testDateParsing() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, dd MMM yyyy HH:mm:ss Z (z)");
String dateString = "Mon, 5 Mar 2001 08:23:00 -0800 (PST)";
ZonedDateTime dateTime = ZonedDateTime.parse(dateString, formatter);
}
which is failing and throwing a DateTimeParseException
and I can't figure out why. Here is the exception:
java.time.format.DateTimeParseException: Text 'Mon, 5 Mar 2001 08:23:00 -0800 (PST)' could not be parsed at index 5
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
at parser.ParserTest.testDateParsing(Parser.java:26)
Any idea what's wrong with the date here? Other dates such as the following work fine:
String dateString = "Wed, 15 Mar 2000 08:52:00 -0800 (PST)";
Upvotes: 1
Views: 160
Reputation: 1494
This code works after removing dd, just use d, tried and worked:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss Z (z)");
String dateString = "Mon, 5 Mar 2001 08:23:00 -0800 (PST)";
ZonedDateTime dateTime = ZonedDateTime.parse(dateString, formatter);
Upvotes: 0
Reputation: 5072
This is because you have 'dd' to represent date but in your sample String you have '5' as date component. Had it been 05 as date than it would have parsed correctly. For your date String, the pattern should be:
DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss Z (z)")
Upvotes: 3