Compare 2 Dates by hours and minutes in Java only?

I am trying to compare 2 days (actually 3). In my use case, I have 3 dates that I need to compare. One of them is StartSleepingTime, StopSleepingTime and Now. My application monitors user activity levels, and I have a schedueld task that runs every half hour to check if there is NOW between the user StartSleepingTime and StopSleepingTime to make sure that the activity monitoring service is stopped during this interval. Since the user sets StartSleepingTime and StopSleepingTime on the first login, when I start the scheduled tasks, both will be in the "past" compared to "NOW". Currently, I am trying to extract only hourly and minute information from three dates and make such a comparison:

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;

}

- . , , . , !

+4
3

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

+4

:

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

}
+2

:

    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

Source: https://habr.com/ru/post/1671273/


All Articles