JacobTStaggs
JacobTStaggs

Reputation: 107

ZonedDateTime.parse could not parse at index 2

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

Answers (2)

Malt
Malt

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 digits
  • MM - month, in 2 digits
  • dd - day, in 2 digits
  • HH - hour, 2 digits in 24 hour format
  • mm - minutes, 2 digits
  • ss - seconds, 2 digits
  • VV - timezone id

The 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

Andrei Tigau
Andrei Tigau

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

Related Questions