Solved

checking a day

Posted on 2013-05-24
17
216 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
nestparen challenge 4 57
base64 decode encode 12 100
Requested array size exceeds VM limit 3 51
github account with ecipse 1 20
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

747 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

9 Experts available now in Live!

Get 1:1 Help Now