Solved

checking a day

Posted on 2013-05-24
17
231 Views
Last Modified: 2013-05-24
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
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39194828
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)
0
 

Author Comment

by:royjayd
ID: 39194835
thanks, let me try
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39194839
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

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:royjayd
ID: 39194863
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39194893
The last line of the output i posted shows what happens when it's run next Monday
0
 

Author Comment

by:royjayd
ID: 39194903
thanks,
can you please tell me how you were able to generate the above log messages
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39194923
Well that's just the effect of running it will a shell script in fact. Is that useful in any way to you?
0
 

Author Comment

by:royjayd
ID: 39194938
>>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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39194984
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?
0
 

Author Comment

by:royjayd
ID: 39195002
>>and that would be the case for all days up to but excluding the Friday of that week?

yes , exactly.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39195090
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

0
 

Author Comment

by:royjayd
ID: 39195108
Yes that is correct.
Thx
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39195122
	public static Date[] getPeriod(Date when) {
		Date[] result = new Date[2];
		Calendar cal = Calendar.getInstance();

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

		while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY) {
			cal.add(Calendar.DATE, -1);
		}

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

		return result;
	}

Open in new window

0
 

Author Comment

by:royjayd
ID: 39195192
thanks
will the parameter to the method 'when' will always be null ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39195205
If you don't need to tie it to a date other than today's, the parameter could be removed
0
 

Author Comment

by:royjayd
ID: 39195257
wonderful!!
You made my day :-)
thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39195573
:)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question