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

查看:25
本文介绍了比较日期是否早于 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天全站免登陆