Solved

checking a day

Posted on 2013-05-24
17
218 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
My project did see openJDK that I installed. What could be the problem 7 123
bitbucket vs gitbucket 3 57
diffSum example 4 26
tomcat not starting 6 31
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now