1

I am getting a parsing error with the following code

String time = "24 Apr 2021 11:56:44";
Date timeOnLine = new SimpleDateFormat("dd MMM yyyy HH:mm:ss").parse(time);

Exception:

java.text.ParseException: Unparseable date: "24 Apr 2021 11:56:44"

I am not sure what the problem is since the pattern seems to correspond with the string correctly.

Any advice on how to solve would be greatly appreciated!

Felix H.
  • 75
  • 1
  • 6
  • 3
    Is "Apr" actually "April" in the default locale of wherever this code is running on? – Federico klez Culloca Apr 24 '21 at 11:24
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 24 '21 at 12:11

1 Answers1

2

Try with:

String time = "24 Apr 2021 11:56:44";
Date timeOnLine = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US).parse(time);
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135