Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare 2 Dates only by Hour and Minute in Java?

I am trying to compare 2 days (actually 3). In my use case, I have 3 dates that I need to compare. One is StartSleepingTime, StopSleepingTime and Now. My application monitors the user's activity levels and I have a schedueld task that runs every half an hour to check if NOW is between the user's StartSleepingTime and StopSleepingTime in order to make sure that the activity minitoring service is stopped during this interval. Since the user set's the StartSleepingTime and StopSleepingTime in upon first login in when I am running the scheduled tasks both will be in the "past" compared to NOW. Currently I am trying to extract only the hour and minute information from the 3 dates and do a comparison like that:

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;

}

But this does not work for some reason. In addition, I it feels like there must be a better way to achieve the comparison. Any suggestion please!

like image 309
Georgi Koemdzhiev Avatar asked Jan 05 '23 10:01

Georgi Koemdzhiev


1 Answers

I use joda-time to handle with dates. So i'd have something like:

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));
    }

}

EDIT

I made a sample if you want to play with: https://github.com/betorcs/jodatime-sample

like image 74
betorcs Avatar answered Jan 13 '23 11:01

betorcs