Java.lang.IllegalArgumentException : Parse error – Date format error? abnormal

Java.lang.IllegalArgumentException : Parse error – Date format error? abnormal … here is a solution to the problem.

Java.lang.IllegalArgumentException : Parse error – Date format error? abnormal

I use the variable CURRENT_DATE to store the current date in the SQLite database. I found that the date format used is yyyy-mm-dd. I want to parse the date in the code, but I get this error :

java.lang.IllegalArgumentException: Parse error:
at java.util.Date.parseError

The code is as follows:

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
    String formattedTaskDate = dateFormat.format(new Date(dateStoredAsStringFetchedFromDB));
    Date d = new Date(formattedTaskDate);

First, I get

the date from the database and store it in a String variable (since the date is stored as TEXT in SQLite), and then I do the above operation, but the exception I get is parseError

How do I fix this?

Solution

Your date format seems to be incorrect. You should note that the uppercase M is used to represent the month and the lowercase M is used to represent the minute. To resolve this issue, simply change yyyy-mm-dd to yyy-mm-dd.

Now, if you want to change the format later, you can do so:

try {
     DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     Date input = inputFormat.parse(date);
     DateFormat outputFormat = new SimpleDateFormat(" EEEE MMMM-dd-yyyy ", Locale.ENGLISH);
     String finaldate = outputFormat.format(input);
            txtDate.setText(finaldate); <-- just an example of displaying the date 
     }catch (Exception ex) {
            Alerts.CatchError(getBaseContext(), ex.toString());
     }

This will display the originally stored date 2015-04-25

12:08:34 (yyyy-MM-dd HH:mm:ss ) as Friday April-25-2015. Of course, you can also change it to your liking by quoting the documentation that Ankit links to for you.

Related Problems and Solutions