-1

The time string is like Tue Feb 09 10:23:31 CST 2021, how to convert to date object? This is my code :

SimpleDateFormat dateFormat = new SimpleDateFormat("E M d hh:mm:ss z yyyy");
Date parsedDate = dateFormat.parse("Tue Feb 09 10:23:31 CST 2021");
System.out.println("===>"+parsedDate.getTime());

It throw the below exception:

java.text.ParseException: Unparseable date: "Tue Feb 09 10:23:31 CST 2021"

at java.text.DateFormat.parse(DateFormat.java:366)
nick wu
  • 139
  • 1
  • 5
  • 17
  • 1
    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 09 '21 at 06:06
  • 2
    Does this answer your question? [Java unparsable date SimpleDateFormat](https://stackoverflow.com/questions/60000869/java-unparsable-date-simpledateformat). Combine with [this](https://stackoverflow.com/questions/49708445/12xx-shown-as-00xx-in-simpledateformat-formathhmmss). Yet a helpful question is [here](https://stackoverflow.com/questions/4713825/how-to-parse-output-of-new-date-tostring). – Ole V.V. Feb 09 '21 at 06:07

1 Answers1

0

Oh I find the root cause, the SimpleDateFormat is wrong.It should be "EEE MMM dd hh:mm:ss zzz yyyy"

        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy");
        Date parsedDate = dateFormat.parse("Tue Feb 09 10:23:31 CST 2021");
        System.out.println("===>"+parsedDate.getTime());

It work now.

nick wu
  • 139
  • 1
  • 5
  • 17