在Java中仅按小时和分钟比较2个日期?

时间:2017-03-02 16:30:53

标签: java android date calendar

我想比较2天(实际上是3天)。在我的用例中,我有3个日期需要比较。一个是StartSleepingTime,StopSleepingTime和Now。我的应用程序监视用户的活动级别,并且我有一个每半小时运行一次的scheduleeld任务,以检查NOW是否在用户的StartSleepingTime和StopSleepingTime之间,以确保在此间隔期间停止活动监视服务。由于用户在首次登录时设置了StartSleepingTime和StopSleepingTime,当我运行计划任务时,与NOW相比,两者都将处于“过去”状态。目前我正在尝试从3个日期中仅提取小时和分钟信息并进行比较:

public static boolean compareHrsAndMintsOnly(Date startSH, Date now, Date stopSH) {
    boolean isNowWithinSleepingTime = false;

    Calendar startSHCalendar = Calendar.getInstance();
    startSHCalendar.setTime(startSH);
    Calendar nowCalendar = Calendar.getInstance();
    nowCalendar.setTime(now);
    Calendar stopSHCalendar = Calendar.getInstance();
    stopSHCalendar.setTime(stopSH);

    int startSHhour = startSHCalendar.get(Calendar.HOUR_OF_DAY);
    int startSHmin = startSHCalendar.get(Calendar.MINUTE);

    int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);
    int nowMin = nowCalendar.get(Calendar.MINUTE);

    int stopSHhour = stopSHCalendar.get(Calendar.HOUR_OF_DAY);
    int stopSHmin = stopSHCalendar.get(Calendar.MINUTE);

    if ((startSHhour > nowHour && startSHmin > nowMin) && (nowHour < stopSHhour && nowMin < stopSHmin)) {
        // stop the monitoring service
        isNowWithinSleepingTime = true;
    }else {
       // start the monitoring service

        isNowWithinSleepingTime = false;
    }

    return isNowWithinSleepingTime;

}

但由于某些原因,这不起作用。另外,我感觉必须有更好的方法来实现比较。有任何建议请!

3 个答案:

答案 0 :(得分:4)

我使用joda-time来处理日期。所以我有类似的东西:

package org.devmaster.sample;

import org.joda.time.DateTime;
import org.joda.time.Interval;

import java.util.Calendar;
import java.util.Date;

public final class Util {

    public static boolean compareHrsAndMintsOnly(Date startSH, Date now, Date stopSH) {
        DateTime start = toDateTime(startSH);
        DateTime end = toDateTime(stopSH);

        Interval interval = new Interval(start, end);

        DateTime instant = toDateTime(now);

        // now is between startSH and stopSH
        return interval.contains(instant);
    }

    private static DateTime toDateTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return new DateTime(
                c.get(Calendar.YEAR),
                c.get(Calendar.MONTH),
                c.get(Calendar.DAY_OF_MONTH),
                c.get(Calendar.HOUR_OF_DAY),
                c.get(Calendar.MINUTE));
    }

}

修改

如果你想玩https://github.com/betorcs/jodatime-sample

我做了一个样本

答案 1 :(得分:3)

试试这个:

    Calendar startSHCalendar = Calendar.getInstance();
    startSHCalendar.setTime(startSH);
    Calendar nowCalendar = Calendar.getInstance();
    nowCalendar.setTime(now);
    Calendar stopSHCalendar = Calendar.getInstance();
    stopSHCalendar.setTime(stopSH);

    int startSHhour = startSHCalendar.get(Calendar.HOUR_OF_DAY);
    int startSHmin = startSHCalendar.get(Calendar.MINUTE);
    int timeStart = startSHhour*60 + startSHmin;  //this

    int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);
    int nowMin = nowCalendar.get(Calendar.MINUTE);
    int timeNow = nowHour*60 + nowMin;  //this

    int stopSHhour = stopSHCalendar.get(Calendar.HOUR_OF_DAY);
    int stopSHmin = stopSHCalendar.get(Calendar.MINUTE);
    int timeStop = stopSHhour*60 + stopSHmin;  //this

    if( timeStart <= timeNow  && timeNow <= timeStop ){
        //between
    }else{
        //not betwwen
    }

/!\警告,如果您在两天之内,您还必须与当天进行比较

希望它会有所帮助

答案 2 :(得分:2)

保持简单:

if(nowHour == stopSHhour && nowMin == stopSHmin){ 
    // do something...

}