如何检查一天是否在一个范围之间?

时间:2014-02-07 07:26:34

标签: java algorithm math calendar dayofweek

我有一份工作,从某一天到一周的某一天。例如(周一至周六)。 当停止日期时,我能够覆盖案例> starday for eg-startday-> Monday stopDay-> Saturday 但当范围变为星期三到星期一时,我无法涵盖这种情况。

private boolean isNotWindow(DateTime todayDate) {
        final int hr = 3600;
        final int min = 60;
        int stopDay =
                Integer.parseInt(ShipmentTrackingEmailProperties.getInstance().getProperty(
                        ShipmentTrackingEmailProperties.SHIPMENT_EMAIL_STOP_DAY));
        int startDay =
                Integer.parseInt(ShipmentTrackingEmailProperties.getInstance().getProperty(
                        ShipmentTrackingEmailProperties.SHIPMENT_EMAIL_START_DAY));

        if(stopDay<startDay)
        {
            //Not able to figure out??????
        }
        if (todayDate.getDayOfWeek() >= stopDay) {
            String stopTime =
                    ShipmentTrackingEmailProperties.getInstance().getProperty(ShipmentTrackingEmailProperties.SHIPMENT_EMAIL_STOP_TIME);
            String[] array = stopTime.split(":");
            System.out.println(array[0] + "  " + array[1] + "   " + array[2]);
            int hh = Integer.parseInt(array[0]);
            int mm = Integer.parseInt(array[1]);
            int ss = Integer.parseInt(array[2]);
            int tSec = todayDate.getHourOfDay() * hr + todayDate.getMinuteOfDay() * min + todayDate.getSecondOfDay();
            int sSec = hh * hr + mm * min + ss;
            if (tSec > sSec) {
                     return true;
            }
        }
        if (todayDate.getDayOfWeek() <= startDay) {
            String startTime =
                    ShipmentTrackingEmailProperties.getInstance().getProperty(ShipmentTrackingEmailProperties.SHIPMENT_EMAIL_START_TIME);
            String[] array = startTime.split(":");
            int hh = Integer.parseInt(array[0]);
            int mm = Integer.parseInt(array[1]);
            int ss = Integer.parseInt(array[2]);
            int tSec = todayDate.getHourOfDay() * hr + todayDate.getMinuteOfDay() * min + todayDate.getSecondOfDay();
            int sSec = hh * hr + mm * min + ss;
            if (tSec <= sSec) {
                LOG.info("Not a valid day to send mail ." + todayDate.getDayOfWeek());
                 return true;
            }
        }
             LOG.info("Valid day to send mail ." + todayDate.getDayOfWeek());
         return false;
    }

如果日期不在一个范围内,则此函数返回true。所以如何覆盖案例时 stopDay&lt;朝九特派

2 个答案:

答案 0 :(得分:1)

您可以使用此功能检查范围内的日期

private static boolean inRange(int startDay, int stopDay, int checkMe) {
    if(startDay==stopDay) {
           if(startDay==checkMe){
                   return true;
           } else {
                   return false;
           }
    }
    while(startDay!=stopDay) {
        if(startDay==checkMe || stopDay==checkMe) {
            return true;
        }
        if(startDay==7) {
            startDay =0;
        }
        startDay++;
    }
    return false;
}

希望它有所帮助。

答案 1 :(得分:1)

你需要:

if startday<stopday then
    if the day is in the interval (startday,stopday) then OK
    else NotOk
else 
    if the day is not in the interval (startday,stopday) then OK
    else NotOk

它可以更容易完成:

If((day-startday)*(stopday-day)*(stopday-startday)>=0) then OK
else NotOk