Simpledateformat unparseable date

Use this date formatter method I have created

    public static String dateFormater(String dateFromJSON, String expectedFormat, String oldFormat) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
    Date date = null;
    String convertedDate = null;
    try {
        date = dateFormat.parse(dateFromJSON);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(expectedFormat);
        convertedDate = simpleDateFormat.format(date);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return convertedDate;
}

and call this method like

dateFormater(" 01/04/2018" , "EE dd MMM yyyy" , "dd/MM/yyyy") 

and you will get the desired output


You need two date formatters here. One to parse the input, and a different formatter to format the output.

SimpleDateFormat inDateFmt = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat outDateFmt = new SimpleDateFormat("EEE dd MMM yyyy");
try {
    Date date = inDateFmt.parse(match.getDate());
    textViewDate.setText(outDateFmt.format(date));
} catch (ParseException ex) {
    System.out.println(ex.toString());
}

The other answers solved your problem, but I think it's important to know some concepts and why your first attempt didn't work.

There's a difference between a date and a text that represents a date.

Example: today's date is March 9th 2018. That date is just a concept, an idea of "a specific point in our calendar system".

The same date, though, can be represented in many formats. It can be "graphical", in the form of a circle around a number in a piece of paper with lots of other numbers in some specific order, or it can be in plain text, such as:

  • 09/03/2018 (day/month/year)
  • 03/09/2018 (monty/day/year)
  • 2018-03-09 (ISO8601 format)
  • March, 9th 2018
  • 9 de março de 2018 (in Portuguese)
  • 2018年3月5日 (in Japanese)
  • and so on...

Note that the text representations are different, but all of them represent the same date (the same value).

With that in mind, let's see how Java works with these concepts.

  • a text is represented by a String. This class contains a sequence of characters, nothing more. These characters can represent anything; in this case, it's a date
  • a date was initially represented by java.util.Date, and then by java.util.Calendar, but those classes are full of problems and you should avoid them if possible. Today we have a better API for that.

In Android, you can use the java.time classes if available in the API level you're using, or the threeten backport for API levels lower than that (check here how to use it). You'll have easier and more reliable tools to deal with dates.

In your case, you have a String (a text representing a date) and you want to convert it to another format. You must do it in 2 steps:

  1. convert the String to some date-type (transform the text to numerical day/month/year values) - that's called parsing
  2. convert this date-type value to some format (transform the numerical values to text in a specific format) - that's called formatting

Why your attempts didn't work:

  • the first attempt gave you the wrong format because you called Date::toString() method, which produces an output (a text representation) in that format (Sun Apr 08 00:00:00 GMT+00:00 2018) - so the parsing was correct, but the formatting wasn't
  • in the second attempt, you used the output pattern (EE dd MMM yyyy, the one you should use for formatting) to parse the date (which caused the ParseException).

For step 1, you can use a LocalDate, a type that represents a date (day, month and year, without hours and without timezone), because that's what your input is:

String input = "01/04/2018";
DateTimeFormatter inputParser = DateTimeFormatter.ofPattern("dd/MM/yyyy");
// parse the input
LocalDate date = LocalDate.parse(input, inputParser);

That's more reliable than SimpleDateFormat because it solves lots of strange bugs and problems of the old API.

Now that we have our LocalDate object, we can do step 2:

// convert to another format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EE dd MMM yyyy", Locale.ENGLISH);
String output = date.format(formatter);

Note that I used a java.util.Locale. That's because the output you want has the day of week and month name in English, and if you don't specify a locale, it'll use the JVM's default (and who guarantees it'll always be English? it's better to tell the API which language you're using instead of relying on the default configs, because those can be changed anytime, even by other applications running in the same JVM).

And how do I know which letters must be used in DateTimeFormatter? Well, I've just read the javadoc.