Results 1 to 7 of 7
- 07-06-2011, 05:56 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 84
- Rep Power
- 0
Calculate no of weeks between dates
Hi,
I have a start date and an end date.
Start date is always a sunday and end date a saturday.
I have to calculate the no of weeks between the start date and end date.
I am confused about how to calculate when the enddate is in another year?
The maximum difference allowed is 26 weeks
I am using Calender.WeekofYear to calculate and then taking the differnce but this is giving wrong
results.
I am not clear about the week of year calculation concept.
For a given year ,The no of weeks is 52 ,sometimes its 53 and the next years first week is sometimes
1st week or at times 52.
Can anyone please explain this concept of finding no of weeks.
Thanks in advance.
- 07-06-2011, 06:01 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Have you read the API documentation for the GregoreanCalendar class (and its parent class, the Calendar class)?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-06-2011, 06:09 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 84
- Rep Power
- 0
Thanks for the reply.I will read and come back with questions if any.
- 07-07-2011, 03:33 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 84
- Rep Power
- 0
I have read and have a few questions.
If my week starts on Sunday and Ends on saturday ,can I get a calender with weeks numbers?(Any document )
Are the week numbers going to be same as that provided by ISO ,ISO treats Monday as the start date or the week numbers will change if my start date for a week is a sunday?
Please help ,I am a lot confused
Thanks
- 07-07-2011, 04:36 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Reread the API documents for those two classes; the Calendar class has a setFirstDayOfWeek( ... ) method (I didn't even know that those days can differ per country ;-) Of course the week numbers change on those days.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-07-2011, 05:57 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 84
- Rep Power
- 0
thanks for the reply.I tried to do it the way mentioned and it is giving me the desired results
int getDateDiff( Date d1, Date d2) {
if( d1.after(d2) ) { // make sure d1 < d2, else swap them
Date temp = d1;
d1 = d2;
d2 = temp;
}
GregorianCalendar c1 = new GregorianCalendar();
c1.setTime(d1);
GregorianCalendar c2 = new GregorianCalendar();
c2.setTime(d2);
for( long i=1; ; i++ ) {
c1.add( Calendar.WEEK_OF_YEAR, 1 ); // add one day, week, year, etc.
if( c1.after(c2) )
return i;
}
I tested it and it is giving me correct weeks.Do you see any problem with this approach?
- 07-07-2011, 06:41 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
using dates in java
By javapink in forum New To JavaReplies: 4Last Post: 02-10-2011, 07:35 AM -
Dates sometimes off by a day?
By saggio in forum Advanced JavaReplies: 13Last Post: 12-08-2010, 04:56 PM -
UTC Dates
By PedroCosta in forum Advanced JavaReplies: 3Last Post: 04-01-2010, 06:08 PM -
JTable and Dates
By paluee in forum AWT / SwingReplies: 0Last Post: 03-30-2009, 08:02 PM -
Help with dates
By bumblyb33 in forum New To JavaReplies: 1Last Post: 02-13-2009, 02:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks