我想比较我的申请中的两个日期
第一个日期将是今天的日期,第二个日期将来自数据库
为了保存第二个日期,我的代码如下:(使用明天的日期来保持简单)
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String tomorrowAsString = dateFormat.format(tomorrow);
所以tomorrowAsString (10-Oct-2015)
存储在我数据库的一个表中
以另一种形式,我检索该日期并将其放入字符串中
String steepingDate = (c.getString(3));
我想我需要将该字符串转换为日期格式,然后使用以下命令获取今天的日期;
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String todayAsString = dateFormat.format(today);
这是我陷入困境的地方,因为我不确定如何将steepingDate
a转换为a Date
,然后比较字符串的实际方法是什么,我尝试了在线查找,但是尝试了很多不同的方法,到目前为止,没有一个有效。我想知道两者中哪一个是刨丝器
编辑:
我结合了一些答案,并提出以下建议;
do {
String steepingDate = (c.getString(3));
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date steepingdate = formatter.parse(steepingDate);
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
if(today.compareTo(steepingdate)>0)
{
CompleteSteeping.add(c.getString(1));
}
i += 1;
}while (c.moveToNext());
但是
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date steepingdate = formatter.parse(steepingDate);
我收到一个错误说明
'不可解析的日期:“ java.util.GregorianCalender [time = 1444461352665,areFieldsSet = true,Ienient = true \ zone = GMT,firstDayOfWeek = 1,minimalDaysInFirstWeek = 1,ERA = 1,YEAR = 2015,MONTH = 9,WEEK_OF_YEAR = 41 ,WEEK_OF_MONTH = 2,DAY_OF_MONTH = 10,DAY_OF_YEAR = 283,DAY_OF_WEEK = 7,DAY_OF_WEEK_IN_MONTH = 2,AM_PM = 0,HOUR = 7,HOUR_OF_DAY = 7,MINUTE = 15,SECOND = 52,MILLISECOND = 665,ZONE_OFFSET = 0 ]”(偏移量为0)
如果您只想比较字符串,则如下所示:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String tomorrowAsString = dateFormat.format(tomorrow);
calendar = Calendar.getInstance();
Date today = calendar.getTime();
String todayAsString = dateFormat.format(today);
if(tomorrowAsString.equals(todayAsString))
System.out.println(true);
else
System.out.println(false);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句