Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

checking a day

Hi guys

we have a java class which invokes a sql and passes the FROM_DATE and TO_DATE to the sql.
The logic to calcualte FROM_DATE and TO_DATE is as follows ::

If the java class is run on a Friday get the Date, example 2013-05-24 and assign it to  TO_DATE
FROM_DATE = TO_DATE - 5 days (which is the monday for that week), so FROM_DATE will be 2013-05-20

If the java class is run AFTER friday
calculate previous friday as TO_DATE
FROM_DATE = TO_DATE - 5 days (which is the monday for that week)

Finally my FROM_DATE and TO_DATE should return values something like this

From_date     To_date
2013-05-20   2013-05-24  (for this week)
2013-05-27   2013-05-31  (for next week)
2013-06-03   2013-06-07  (and so on..)

Any idea how i can accomplish this?

thanks
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this:

	public static Date[] getPeriod(Date when) {
		Date[] result = new Date[2];
		Calendar cal = Calendar.getInstance();

		if (when != null) {
			cal.setTime(when);
		}

		cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
		result[1] = cal.getTime();
		cal.add(Calendar.DATE, -4);
		result[0] = cal.getTime();

		return result;
	}

Open in new window

(For normal execution, pass null)
Avatar of Jay Roy

ASKER

thanks, let me try
The following shows the dates range when run on the date stated first:

Run on 2013-05-20: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-21: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-22: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-23: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-24: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-25: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-26: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-27: [Mon May 27 00:00:00 BST 2013, Fri May 31 00:00:00 BST 2013]

Open in new window

Avatar of Jay Roy

ASKER

just ran this code today (friday)
result[0] =  Mon May 20 12:25:43 EDT 2013
result[1] = Fri May 24 12:25:43 EDT 2013

which is fine..
now if i run this same code on monday will i get the same result?
thanks
The last line of the output i posted shows what happens when it's run next Monday
Avatar of Jay Roy

ASKER

thanks,
can you please tell me how you were able to generate the above log messages
Well that's just the effect of running it will a shell script in fact. Is that useful in any way to you?
Avatar of Jay Roy

ASKER

>>Well that's just the effect of running it will a shell script in fact. Is that useful in any way to you?
yeah, but i wont divert from the topic :) , here is what i have

If its run on Mon May 27
it should get the data for previous week and not the current week
so it should be
[Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]

so what i am anticipating is

Run on 2013-05-20: [Mon May 13 00:00:00 BST 2013, Fri May 17 00:00:00 BST 2013]
Run on 2013-05-21: [Mon May 13 00:00:00 BST 2013, Fri May 17 00:00:00 BST 2013]
Run on 2013-05-22: [Mon May 13 00:00:00 BST 2013, Fri May 17 00:00:00 BST 2013]
Run on 2013-05-23: [Mon May 13 00:00:00 BST 2013, Fri May 17 00:00:00 BST 2013]
Run on 2013-05-24: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-25: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-26: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-27: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]

..and so on

thanks.
If its run on Mon May 27
it should get the data for previous week and not the current week
and that would be the case for all days up to but excluding the Friday of that week?
Avatar of Jay Roy

ASKER

>>and that would be the case for all days up to but excluding the Friday of that week?

yes , exactly.
OK, then i think you should get this output running from last Monday to Friday the 31st, inclusive?

Run on 2013-05-20: [Mon May 13 00:00:00 BST 2013, Fri May 17 00:00:00 BST 2013]
Run on 2013-05-21: [Mon May 13 00:00:00 BST 2013, Fri May 17 00:00:00 BST 2013]
Run on 2013-05-22: [Mon May 13 00:00:00 BST 2013, Fri May 17 00:00:00 BST 2013]
Run on 2013-05-23: [Mon May 13 00:00:00 BST 2013, Fri May 17 00:00:00 BST 2013]
Run on 2013-05-24: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-25: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-26: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-27: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-28: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-29: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-30: [Mon May 20 00:00:00 BST 2013, Fri May 24 00:00:00 BST 2013]
Run on 2013-05-31: [Mon May 27 00:00:00 BST 2013, Fri May 31 00:00:00 BST 2013]

Open in new window

Avatar of Jay Roy

ASKER

Yes that is correct.
Thx
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

thanks
will the parameter to the method 'when' will always be null ?
If you don't need to tie it to a date other than today's, the parameter could be removed
Avatar of Jay Roy

ASKER

wonderful!!
You made my day :-)
thanks.
:)