比较日期是否早于 24 小时
我正在尝试比较 Java 中的两个日历,以确定其中一个是否 >= 24 小时前.我不确定实现这一目标的最佳方法.
I am trying to compare two calendars in java to decide if one of them is >= 24 hours ago. I am unsure on the best approach to accomplish this.
//get todays date
Date today = new Date();
Calendar currentDate = Calendar.getInstance();
currentDate.setTime(today);
//get last update date
Date lastUpdate = profile.getDateLastUpdated().get(owner);
Calendar lastUpdatedCalendar = Calendar.getInstance();
lastUpdatedCalendar(lastUpdate);
//compare that last hotted was < 24 hrs ago from today?
推荐答案
你可以使用 Date.getTime(),这里是一个例子:
you could use Date.getTime(), here's an example:
public final static long MILLIS_PER_DAY = 24 * 60 * 60 * 1000L;
public static void main(String args[]) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2009-12-31");
Date date2 = sdf.parse("2010-01-31");
boolean moreThanDay = Math.abs(date1.getTime() - date2.getTime()) > MILLIS_PER_DAY;
System.out.println(moreThanDay);
}
相关文章