0

I have date string like Tuesday , December 25th, 1900, how can I covnert it into MM/dd/yyyy format. I have refereed this link but with a workaround for my date format. Below is what I tried but I'm getting an exception as Unparsable date.

String caseDate = "Tuesday , December 25th, 1900";
SimpleDateFormat inputFormat = new SimpleDateFormat("EEEE, MMMM dd Z yyyy");
Date date = inputFormat.parse(caseDate);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(date);
Utils.logtMsg("formattedDate "+formattedDate);

Exception as

Exception ::: Unparseable date: " Tuesday , December 25th, 1900"

Prasad_Joshi
  • 642
  • 3
  • 13
  • 34
  • There might be an issue because of the etra space after your tuesday. Try this String caseDate = "Tuesday , December 25th, 1900"; SimpleDateFormat inputFormat = new SimpleDateFormat("EEEE , MMMM dd Z yyyy"); – Pooja Aggarwal Nov 19 '18 at 06:49
  • Please refer this url https://www.google.co.in/amp/s/www.journaldev.com/17899/java-simpledateformat-java-date-format/amp – GauravRai1512 Nov 19 '18 at 06:51
  • 1
    how does your input format match the input string? I mean, did you compare the pattern to the string? `Z` is timezone, multiple commas, etc – UninformedUser Nov 19 '18 at 06:52
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 19 '18 at 08:14

1 Answers1

6

The issue is that your input string does not match your input format. I changed the format and it is working fine. Try,

String caseDate = "Tuesday , December 25th, 1900";
        SimpleDateFormat inputFormat = new SimpleDateFormat("EEEE , MMMM dd'th', yyyy");
        Date date = inputFormat.parse(caseDate);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = formatter.format(date);
        System.out.println(formattedDate);
Pooja Aggarwal
  • 1,163
  • 6
  • 14