Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: Parse error - Date format error?

I am storing the current date in SQLite db using the variable CURRENT_DATE. I found that the date format used is yyyy-mm-dd in the same. 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 shown below:

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

At first, I am fetching the date from the database and storing it in a String variable (because date is stored as TEXT in SQLite) and then I am performing the above operations but I get the exception as parseError.

How can I solve this issue?

like image 503
Java Enthusiast Avatar asked Aug 22 '26 11:08

Java Enthusiast


2 Answers

It seems your date format is wrong. You should take note that the uppercase M is used to represent months and the lowercase m is used for minutes. To solve this just change your yyyy-mm-dd to yyy-MM-dd.

Now if you want to change that format in the future you may do so by doing something like this :

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 initially stored date of 2015-04-25 12:08:34 ( yyyy-MM-dd HH:mm:ss ) as Friday April-25-2015. You can of course change this to your liking as well, just refer to the documentation Ankit has kindly linked for you.

like image 180
Cytus Avatar answered Aug 24 '26 00:08

Cytus


Format is wrong, try this:

String string = dateStoredAsStringFetchedFromDB;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(string);

Documentation.

like image 32
Ankit Kumar Avatar answered Aug 24 '26 01:08

Ankit Kumar