0

I am trying to parse dates with this format "Sun, 21 Feb 2021 22:13:42" and i am getting a runtime error saying "Unparseable date". Does anyone know why? Here is my parse method.

public Date parseDate(String date) {

       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
        try {
            return simpleDateFormat.parse(date);
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Date();
        }
    }
}
  • `@SuppressLint("SimpleDateFormat")` should make you think a little... Why do you have to suppress a warning about the `SimpleDateFormat`? The answer is: You shouldn't be using this class nowadays because there is `java.time` now. – deHaar Apr 08 '21 at 17:39
  • @deHaar when i remove that it still does not parse – programmingbro Apr 08 '21 at 17:40
  • That's a different problem... Have you compared the pattern with the formatting of the `String` passed? Hint: They don't match (at all). – deHaar Apr 08 '21 at 17:41
  • @deHaar do you know how i can edit the format to parse it? The other question answer suggested for this post does not help me – programmingbro Apr 08 '21 at 17:42
  • @deHaar i know they do not match im unsure how to get them to match as i am new to this. help would be appreciated – programmingbro Apr 08 '21 at 17:43
  • @programmingbro - All of the suggestions by deHaar are valuable. Don't take a shortcut; study [Parsing and Formatting](https://docs.oracle.com/javase/tutorial/datetime/iso/format.html) thoroughly. – Arvind Kumar Avinash Apr 08 '21 at 17:43
  • @ArvindKumarAvinash cant you help me with the format? – programmingbro Apr 08 '21 at 17:45
  • 1
    Not exactly, but I can give you some hints: `Sun, 21 Feb 2021 22:13:42` consists of an abbreviated day of week, a comma, two digits for the day of month, an abbreviated month name, a year and a time of day in 24h-format. The pattern you are using for parsing consists of two digits for day of month, a slash, two digits for month, another slash and the year (4 digits). Now compare them (and consult some documentation, please). – deHaar Apr 08 '21 at 17:45
  • That means the parser expects two digits first (day of month), but the `String` starts with letters representing a day of week. – deHaar Apr 08 '21 at 17:46
  • im not sure how to do this, i am new to this and really struggling. can you tell me? @deHaar – programmingbro Apr 08 '21 at 17:48

0 Answers0