0

I am trying convert String to Date but getting this error.

java.text.ParseException: Unparseable date: "Wed Feb 16 20:04:17 EET 2022"
at java.text.DateFormat.parse(DateFormat.java:366)
at com.mycompany.facebookproject.OpenFace.main(OpenFace.java:55)

My code is:

String val= "Wed Feb 16 20:04:19 EET 2022";
expiry=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(val);
  • Your code works, check this out: https://repl.it/repls/BlondChartreuseSlope – Vüsal Feb 17 '20 at 18:13
  • What is `val` in `parse()`? – Giorgi Tsiklauri Feb 17 '20 at 18:14
  • 1
    The important part of the duplicate is: *Note the importance of the explicit `Locale` argument. If you omit it, then it will use the default locale which is not necessarily English as used in the month name of the input string. If the locale doesn't match with the input string, then you would confusingly get a `java.text.ParseException` even though when the format pattern seems valid.* --- To fix, add `Locale.US` as second parameter to `SimpleDateFormat` constructor. – Andreas Feb 17 '20 at 18:17
  • Its EET for which I guess locale should be different – Dhrubajyoti Gogoi Feb 17 '20 at 18:18
  • @DhrubajyotiGogoi No, it's `Wed`. And `Feb`. And `EET`, so you're partially correct. E.g. in German, that text input would be `Mi. Feb. 16 20:04:19 OEZ 2022`, and in French it would be `mer. févr. 16 20:04:19 EET 2022` – Andreas Feb 17 '20 at 18:19
  • 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 `ZonedDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Feb 17 '20 at 20:40

1 Answers1

0

I guess its a typo:

String dateSt= "Wed Feb 16 20:04:19 EET 2022";
        Date parse = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(dateSt);
        System.out.println(parse);
Dhrubajyoti Gogoi
  • 1,265
  • 10
  • 18