Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

checking a day

Posted on 2013-05-24
17
Medium Priority
?
237 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

877 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