在Android的日期比较 [英] Date comparison in android
本文介绍了在Android的日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在面临在比较当前日期一些问题,这是从Database.I检索刚刚从检索数据库的日期,并存储在像这样的日期变量的日期
I'm facing some problems in comparing the current date and the date which is retrieved from Database.I just retrieved date from DataBase and Stored in a Date variable like this
String due_date_task = cursor.getString(cursor.getColumnIndex(dueDateOfTask));
SimpleDateFormat currentFormater = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = currentFormater.parse(due_date_task);
现在,我想是应该检查这是从数据库中检索日期是否等于或的currentdate没有。
Now,what i want is should check whether date which is retrieved from DataBase is Equivalent to CurrentDate or not.
Calendar currentDate = Calendar.getInstance();
Date date2 = currentDate.getTime();
if(date1.equals(date2))
的System.out.println(今天的任务);
System.out.println("Today Task");
我只是想检查像this.Thanks提前
i just want to check like this.Thanks in advance
推荐答案
您可以使用此功能:
private boolean compareDates(Calendar objCal1, Calendar objCal2) {
return ((objCal1.get(Calendar.YEAR) == objCal2.get(Calendar.YEAR))
&& (objCal1.get(Calendar.DAY_OF_YEAR) == objCal2.get(Calendar.DAY_OF_YEAR)));
}
创建日历对象:
Calendar objCal1 = new GregorianCalendar().setTime(date);
Calendar objCal2 = new GregorianCalendar().setTime(date1);
这篇关于在Android的日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文