java.lang.IllegalArgumentException:解析错误 - 日期格式错误? [英] java.lang.IllegalArgumentException: Parse error - Date format error?
问题描述
使用变量CURRENT_DATE将当前日期存储在SQLite数据库中。我发现使用的日期格式是 yyyy-mm-dd
在同一个。我想解析代码中的日期,但是我收到这个错误:
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:解析错误:
在java.util.Date.parseError
java.lang.IllegalArgumentException: Parse error: at java.util.Date.parseError
代码如下所示:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
String formattedTaskDate = dateFormat.format(new Date(dateStoredAsStringFetchedFromDB));
Date d = new Date(formattedTaskDate);
首先,我从数据库中获取日期并将其存储在一个String变量中(因为日期在SQLite中存储为TEXT),然后我执行上述操作,但是我得到例如 parseError 。
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?
推荐答案
似乎您的日期格式错误。您应该注意,大写 M
用于表示月份,而小写 m
用于分钟。要解决这个问题,只需将您的 yyyy-mm-dd
更改为 yyy-MM-dd
。
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());
}
这将显示最初存储的日期 2015- 04-25 12:08:34
(yyyy-MM-dd HH:mm:ss)as Friday April-25-2015
。您当然可以根据自己的喜好改变这一点,只需参考Ankit与您的联系。
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.
这篇关于java.lang.IllegalArgumentException:解析错误 - 日期格式错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!