java.lang.IllegalArgumentException异常:解析错误 - 无法在Android的解析日期 [英] java.lang.IllegalArgumentException: Parse error - Unable to parse Date in Android
问题描述
我存储SQLite的当前日期分贝使用变量CURRENT_DATE。我发现使用的日期格式是 YYYY-MM-DD
同在。我要处理在code中的日期,但我得到这个错误:
I am storing the current date in SQLite db using the variable CURRENT_DATE. I found the date format used is yyyy-mm-dd
in the same. I want to process 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
在code如下所示:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
String formattedTaskDate = dateFormat.format(new Date(dateStoredAsStringFetchedFromDB));
Date d = new Date(formattedTaskDate);
首先,我从数据库中获取的日期和它在一个字符串变量存储(因为日期存储为SQLite中TEXT),然后我执行上述操作,但我得到的异常为 parseError 。
我要如何解决这个问题?
How do I solve this issue?
推荐答案
看来你的日期格式是错误的。你应该注意的是大写 M
是用来重新present个月,小写 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年4月25日12时08分34秒
(YYYY-MM-DD HH:MM:SS)的初始存储日期周五4月-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异常:解析错误 - 无法在Android的解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!