Android-比较两个日期 [英] Android - Compare two dates
问题描述
我想比较应用程序中的两个日期
I want to compare two dates in my application
第一个日期将是今天的日期
第二个日期将来自数据库
The first date will be todays date The second will come from a database
要保存第二个日期,我的代码如下: (使用明天的日期来保持简单)
To save the second date my code is as follows; (Used tomorrows date to keep it simple)
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(2015年10月10日)
存储在数据库的表中
So tomorrowAsString (10-Oct-2015)
is stored in a table in my database
以另一种形式,我将该日期检索并放入字符串中
In another form I'm retrieving that date and putting it into a String
String steepingDate = (c.getString(3));
我想我需要将该字符串转换为日期格式,然后使用以下命令获取今天的日期;
I imagine that I'll need to convert that string into a date format and then use the following to get todays date;
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String todayAsString = dateFormat.format(today);
这是我被卡住的地方,因为我不确定如何转动 steepingDate
放入 Date
,然后比较字符串的实际方法是什么,我尝试过在线查找,但有很多不同之处我尝试过的方法,到目前为止都没有奏效。我想知道两者中的哪一个是磨丝器
Here is where I'm getting stuck as I'm not sure how to turn the steepingDate
into a Date
and then what is the actual way of comparing the strings, I've tried looking online but there's so many different ways that I've tried and none so far have worked. I want to know which one of the two is grater
编辑:
我已经结合了一些答案并提出以下建议;
I've combined some of the answers and come up with the following;
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,MILLISED_SET = 665,ZONE_OFF = 0](偏移量为0)
'Unparseable date: "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=0DST_OFFSET=0]" (at offset 0)
推荐答案
如果您只想比较字符串,它看起来像这样:
If you just want to compare the Strings it would look like this:
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);
这篇关于Android-比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!