比较日期是否小于24小时 [英] Compare if a date is less than 24 hours before

查看:142
本文介绍了比较日期是否小于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);
}

这篇关于比较日期是否小于24小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆