我有一个问题。如何比较2日期和时间
enter code here
if(fromdate<=nowdt.now() && todate>= nowdt.now()){
////
}
答案 0 :(得分:11)
java.util.Date对象包含用于比较日期的方法.before(),. after和.equals()。
if((fromdate.before(nowDt) || fromDate.equals(nowDt))
&& ((todate.after(nowDt) || toDate.equals(nowDt))
////
}
答案 1 :(得分:0)
比较两个日期和时间的功能:
public static int compareTwoDates(Date date1, Date date2) {
if (date1 != null && date2 != null) {
int retVal = date1.compareTo(date2);
if (retVal > 0)
return 1; // date1 is greatet than date2
else if (retVal == 0) // both dates r equal
return 0;
}
return -1; // date1 is less than date2
}
您可以在任意位置使用它。结果将是> 0如果date1&gt; date2,如果date1 = date2,则为0,&lt;如果date1&lt; 0 DATE2。希望它有所帮助。
答案 2 :(得分:0)
- 使用Joda Time library
执行此操作....
<强>例如强>
Date ds = new Date();
DateTime d = new DateTime(ds);
DateTime e = new DateTime(2012,12,07, 0, 0);
System.out.println(d.isEqual(e));
System.out.println(d.toDateMidnight().isEqual(e.toDateMidnight()));
/////////////////////////////或
System.out.println(d.withTimeAtStartOfDay().isEqual(e.withTimeAtStartOfDay()));
答案 3 :(得分:0)
尝试一下以找出时差
Calendar Day = Calendar.getInstance();
Day.set(Calendar.DAY_OF_MONTH,25);
Day.set(Calendar.MONTH,7);
Day.set(Calendar.YEAR, 1985);
Calendar today = Calendar.getInstance();
long diff = today.getTimeInMillis() - Day.getTimeInMillis();
答案 4 :(得分:0)
你可以写
long nowTime = System.currentTimeMillis();
if(fromdate.getTime() <= nowTime && nowTime <= todate.getTime()) {
或者你可以写
Date nowDate = new Date();
if(fromdate.compareTo(nowDate) * nowDate.compareTo(todate) >= 0) {
或
if(!fromdate.after(nowDate) && !todate.before(nowDate))
答案 5 :(得分:0)
使用此答案
if((options.FromDate.before(now_Date)||options.FromDate.equals(now_Date)) && (options.ToDate.after(now_Date)|| options.ToDt.equals(now_Date)) ){
do some processs........
}