0

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

Gangnus
  • 24,044
  • 16
  • 90
  • 149
Raman Singh
  • 329
  • 7
  • 17
  • I have not look at your code. But, I suggest that you try a joda time API. I have a many easy to use functions for date related tasks. Chenqui. – Erran Morad Feb 07 '14 at 07:28
  • Is using `JodaTime` an option? If so, you can define an `Interval` and check if it contains your date. – Keppil Feb 07 '14 at 07:29
  • Here is a SO post which tell us how you do with a Joda - http://stackoverflow.com/questions/883060/how-can-i-determine-if-a-date-is-between-two-dates-in-java It is a very easy. – Erran Morad Feb 07 '14 at 07:29
  • Date manipulations are fine . But how to work with days and time and check if a certain day,time falls between a range. – Raman Singh Feb 07 '14 at 07:30
  • All those functions are for date checking. I want to check if for eg . Saturday falls b/w a range of Wednesday to Monday – Raman Singh Feb 07 '14 at 07:31
  • @RamanSingh - I make wrong choice of words. It have a much more functionality than the standard Java Date API. See link, try and let us know. – Erran Morad Feb 07 '14 at 07:31
  • 1
    @BoratSagdiyev - I think you understood the question wrong . I have a DAY (day of a week) and a time and i want to find out if it lies b/w two given DAYOFWEEK and TIME. – Raman Singh Feb 07 '14 at 07:34
  • What is todayDate ? Joda DateTime? – assylias Feb 07 '14 at 07:57
  • @BoratSagdiyev the op already uses joda it seems... – assylias Feb 07 '14 at 07:57
  • yeah todayDate is DateTime Object from JODA Api – Raman Singh Feb 07 '14 at 08:01

2 Answers2

1

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.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
  • That's correct sir, but could you suggest edit in my code . That will be helpful. Thanks.. – Raman Singh Feb 07 '14 at 08:42
  • You can use the inRange function to find out if your day is in startday and stopday range. If it falls in day range than you can calculate start time and end time and see if todayDate's time falls in that range or not. – Sanjeev Feb 07 '14 at 08:56
1

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
Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • The only thing I added was I had to take in account the time also So I calculated seconds in a days as (dayOfWeek-1)*24*3600 + no. of seconds and compared them instead of day. – Raman Singh Feb 07 '14 at 15:42
  • 1
    @RamanSingh Of course. I only wanted to show the main thought... I love logic, based on counting :-) – Gangnus Feb 07 '14 at 15:49