Parsing specific String to Date

Eltomon :

Throughout reading a whole table via Selenium and writing the result to an excel file , I am currently facing a problem formatting/parsing a date String in a Date object. What I want to archive is the following format:

dd-mm-yyyy

The dateString which is retrieved from the table look like this

16 APR 2020

I tried to use the SimpleDateFormat formatter but I am getting a ParseException.

java.text.ParseException: Unparseable date: "16 Apr 2020"
deHaar :

Just for the record: This is how to parse and format a date String nowadays with java.time (available from Java 8):

public static void main(String[] args) {
    // the date String
    String d = "16 Apr 2020";
    // which is parsed to a LocalDate using a formatter with a suitable pattern
    LocalDate ld = LocalDate.parse(d, DateTimeFormatter.ofPattern("dd MMM yyyy"));
    // and which is then printed in a different format using a formatter with a differen pattern
    System.out.println(ld.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
}

The output of this is

16-04-2020

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=334328&siteId=1