下面是我用来从数据库解析日期的代码: -
String myFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
Date savedDate,currentDate;
for (int i = 0; i < rem_lst.size(); i++)
{
// Toast.makeText(this, ""+rem_lst.get(i).rem_date, Toast.LENGTH_LONG).show();
try
{
String str = rem_lst.get(i).rem_date;
//savedDate = sdf.parse(str);
savedDate = sdf.parse(str);
//currentDate = calendar.getTime();
currentDate = sdf.parse("2014/04/02 15:30:53");
// if(savedDate.compareTo(currentDate)>0){
// System.out.println("Date1 is after Date2");
// }
// if(savedDate.compareTo(currentDate)<0){
// System.out.println("Date1 is before Date2");
// }
if(savedDate.compareTo(currentDate)==0){
System.out.println("Date1 is equal to Date2");
showNotifacation();
}
System.out.println("Date1 is not equal to Date2");
}catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
但由于无法解析日期,我得到例外。
插入日期的代码是: -
str_Rem_Date = editText_Date.getText().toString();
str_Rem_Catagory = ma.GetItem(spinner_Rem_Catagory.getSelectedItemPosition());
if(editText_Date.length()==0)
{
showAlert();
}
else
{
DBAdapter adapter = new DBAdapter(getActivity());
adapter.insertReminder(str_Rem_Date, str_Rem_Catagory);
}
有人请告诉我如何解决此错误。 提前致谢!
答案 0 :(得分:2)
改变这个:
currentDate = sdf.parse("2014/04/02 15:30:53");
到此:
currentDate = sdf.parse("2014-04-02 15:30:53");
答案 1 :(得分:0)
您必须使用与DB日期一起检查的相同日期格式。
字符串myFormat =&#34; yyyy / MM / dd HH:mm:ss&#34 ;;