Question

I have a job which runs from a given day to a certain day of week . For eg (Monday to Saturday). I was able to cover the case when stopday > starday for eg-startday->Monday stopDay->Saturday but when the range changes to something like Wednesday to Monday , I am not able to cover this case.

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

This function returns true if the day does not fall in a range.So how to cover the case when
stopDay < startDay

Was it helpful?

Solution

You need:

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

It could be much easier done as:

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

OTHER TIPS

You can use this function to check for the day in range

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

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top