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?
Could you please paste the value of
dateStoredAsStringFetchedFromDB
as returned from the database ? It's probable that the returned value does not match the yyyy-mm-dd format.Also please note that from the javadoc (https://developer.android.com/reference/java/text/SimpleDateFormat.html) the 'm' pattern denotes minutes and not month as you seem to expect.
It seems your date format is wrong. You should take note that the uppercase
M
is used to represent months and the lowercasem
is used for minutes. To solve this just change youryyyy-mm-dd
toyyy-MM-dd
.Now if you want to change that format in the future you may do so by doing something like this :
This will display the initially stored date of
2015-04-25 12:08:34
( yyyy-MM-dd HH:mm:ss ) asFriday 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.Format is wrong, try this:
Documentation.