Reputation: 107
After looking at similar questions and solutions on here I have not been capable of successfully parse a date. I am trying to parse the date like so below. But whenever it gets called it throws the error
java.time.format.DateTimeParseException: Text '2019-08-05 23:59:00America/Chicago' could not be parsed at index 2
Here is the code I am talking about. Any insight or tips would be greatly appreciated.
ZonedDateTime.parse("2019-08-05 23:59:00America/Chicago", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mmVV"));
Upvotes: 0
Views: 516
Reputation: 30335
Your pattern is completely wrong. It doesn't match the format you're trying to parse.
The following pattern would work: yyyy-MM-dd HH:mm:ssVV"
.
Example:
public static void main(String[] args) throws FileNotFoundException {
ZonedDateTime parse = ZonedDateTime.parse("2019-08-05 23:59:00America/Chicago", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssVV"));
System.out.println(parse);
}
This code prints 2019-08-05T23:59-05:00[America/Chicago]
Explanation:
yyyy
- year, in 4 digitsMM
- month, in 2 digitsdd
- day, in 2 digitsHH
- hour, 2 digits in 24 hour formatmm
- minutes, 2 digitsss
- seconds, 2 digitsVV
- timezone idThe code you posted fails at index 2 because it expects day-of-month value in 2 digits (dd
) for which it sees 20
and then, at index 2, it expects a forward slash (/
) which you don't have.
Upvotes: 3
Reputation: 2058
You can try this. Theoretically what you have posted should have worked, also.
DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime dt = fmt.parseDateTime(string);
Upvotes: 0