Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# how to find how many sundays comes in 2 Dates

ganesh pol
Ranch Hand
Posts: 151
initially i have 2 Dates in String Format
"2005-Aug-12" and "2004-Apr-14"

i have converted String in Dates as follows

public static Date getDate (String date) throws Exception {
//log.debug(" "+date);
return SDF.parse(date);
}

public static long getDiffInDays(Date d1,Date d2){
long diffInMilliSeconds=d1.getTime()-d2.getTime();
//long diffInDays=diffInMilliSeconds/(1000*60*60*24);
return diffInMilliSeconds/(1000*60*60*24);
}

but then i am confused how to calculate number of Sundays between these two Dates when i have Time period also please tell me logic of how to calculate it

Seb Mathe
Ranch Hand
Posts: 225
Assuming d1 and d2 are your 2 Date objects :

Tom Blough
Ranch Hand
Posts: 263
Divide the number of days by 7 and add 1 if the first or last day falls on Sunday.

Cheers,

ganesh pol
Ranch Hand
Posts: 151
hi friend thanks for giving me reply but i have got following o/p for your code

public static int getNumberofSundays(String d1,String d2) throws Exception{ //object in Date form
Date date1=getDate(d1);
Date date2=getDate(d2);

Calendar c1=Calendar.getInstance();
c1.setTime(date1);
Calendar c2=Calendar.getInstance();
c2.setTime(date2);
int sundays=0;
while(c1.after(c2)){
if(c2.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
sundays++;
}
}

System.out.println("number of days between 2 dates"+sundays);

return sundays;
}

public static void main(String[] arg)throws Exception{
System.out.println(" "+getNumberofSundays("2005-Oct-07","2006-Mar-01"));
}
ihave following o/p

Microsoft Windows 2000 [Version 5.00.219

C:\>javac DateDiffCalculator.java

C:\>java DateDiffCalculator
number of days between 2 dates0_________________o/p due to s.o.p in method
0_______________________________dut to s.o.p in main method
C:\>

ganesh pol
Ranch Hand
Posts: 151
thanks Tom
it is simple logic to impliment
but after division numbers come in fraction Form what to do with it

public static int getNumberofSundays(String d1,String d2) throws Exception{
//d1 is leave start date d2 is leave end date

// get object in Date form
Date date1=getDate(d1);
Date date2=getDate(d2);
long leavePeriod = Math.abs(getDiffInDays(date1,date2));

float noOfWeeks=leavePeriod/7;

System.out.println("no of weeks"+noOfWeeks+"remainder "+remainder);

return (int)noOfWeeks;
}

public static long getDiffInDays(Date d1,Date d2){

long diffInMilliSeconds=d1.getTime()-d2.getTime();
//dividing diffInMilliSeconds by 1000 to convert milliseconds to seconds
//by first sixty to get diff in minutes
//by first sixty to get diff in hours
//by 24 to get Diff in days

//long diffInDays=diffInMilliSeconds/(1000*60*60*24);
return diffInMilliSeconds/(1000*60*60*24);

}

how to consider that fraction which comes after division

Ryan McGuire
Ranch Hand
Posts: 1061
4
Originally posted by Tom Blough:
Divide the number of days by 7 and add 1 if the first or last day falls on Sunday.

Cheers,

But that doesn't work in all cases.

How many Sundays are there between Oct 7, 2005 and Oct 10, 2005?
10-7=3. (int)(3/7) = 0. Neither the first nor last day falls on a Sunday, so don't add 1. That yields an answer of 0 Sundays, but there is in fact 1 Sunday between those two dates.

Can you see a relatively easy way to fix this?
[ October 07, 2005: Message edited by: Ryan McGuire ]

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Starting day-of-week + number-of-days + 6 / 7 ... plus 1 if starting day is a Sunday?

Michael Dunn
Ranch Hand
Posts: 4632
untested
(d2-d1)/7 + (weekday(d2) <= weekday(d1)? 1:0)

John D. Lowe
Greenhorn
Posts: 4
Hum. What if leap day lands on a Sunday between the two dates? I like to idea of using a algebraic approach. My experience is that simple date equations almost never work- there's always an exceptional case that breaks the model. I do think there is a set of expressions to determine this quickly, though the iterative approach might be easier to understand (ah, the classic battle...).

Kenneth Albertson
Ranch Hand
Posts: 59
I think that everyone is making the same mistake, which is a variation on the fence-post error. The actual problem statement is: "calculate number of Sundays between these two Dates". You need to count whole days between the start and finish dates. The start and finish days are not included.

Assuming that the dates (with or without times) are converted to GregorianCalendar values:

period = end_date - start_date - (time to end of day in start date) - (time from start of day in end date)

In whole days, this reduces to period = d2 - d1 - 1

Now divide by milliseconds per week. The whole interger result is the number of 7-day periods, which have 1 Sunday each. The remainder (divided by milliseconds per day) gives the number of extra whole days. Then:

IF d1.DAY_OF_WEEK + extra days >= Calendar.SUNDAY
THEN Sundays++

I think this algorithm will work in all cases, even over month ends. Leap years make no difference.

The Java version of the algorithm is left as an exercise.

Michael Dunn
Ranch Hand
Posts: 4632
> The start and finish days are not included.

perhaps yes, perhaps no.

If you were to ask a group of people how many days between monday and friday,

Basically depends on the specific requirements, which weren't stated.

ganesh pol
Ranch Hand
Posts: 151
Thanks Seb Mathe

thanks for taking time for typing code for me

public static int getNumberOfWeekEnds(String d1,String d2)throws Exception {
Date date1=getDate(d1);
Date date2=getDate(d2);

Calendar c1 = Calendar.getInstance();
c1.clear();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
c2.clear();
c2.setTime(date2);
int WeekEnds = 0;

while(c2.after(c1)) {
if(c1.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||
c1.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)
WeekEnds++;

}
System.out.println(WeekEnds);
return WeekEnds;

}

when from Main metod i call this Method with two different Dates

public static void main(String[] arg)throws Exception{
System.out.println("for two diff Dates "+ getNumberOfWeekEnds("2005-Oct-09","2005-Nov-09"));
System.out.println("for Two Same Dates "+getNumberOfWeekEnds("2005-Oct-09","2005-Oct-09"));
}

//____o/p is

C:\>java DateDiffCalculator
9
for two diff Dates 9
0
for Two Same Dates 0

C:\>

how to resolve this problem

Kenneth Albertson
Ranch Hand
Posts: 59
It is always good to start by clarifying what the problem is. Then writing some test cases. Then doing some design. Then some coding. Then some testing.

I see from your latest post ganesh that you are now counting weekends rather than Sundays. Or rather, you have a field called WeekEnds, but you are counting Saturdays and Sundays. [And, to continue my fenceposts argument, the first day counts if it is a Sat or Sun, but the last day does not.]

So let me ask you, how many weekends are there between:
• Thu 6 Oct & Tue 11 Oct
• Fri 7 Oct & Sat 8 Oct
• Fri 7 Oct & Sun 9 Oct
• Fri 7 Oct & Mon 10 Oct
• Sat 8 Oct & Sun 9 Oct
• Sat 8 Oct & Mon 10 Oct
• Sun 9 Oct & Sun 9 Oct

• I haven't run your code, but it looks to me as if it will produce these results
• Thu 5 Oct & Tue 10 Oct - 2
• Fri 7 Oct & Sat 8 Oct - 0
• Fri 7 Oct & Sun 9 Oct - 1
• Fri 7 Oct & Mon 10 Oct - 2
• Sat 8 Oct & Sun 9 Oct - 1
• Sat 8 Oct & Mon 10 Oct - 2
• Sun 9 Oct & Sun 9 Oct - 0

• Are those the answers that you expect?
[ October 10, 2005: Message edited by: Kym Thompson ]

ganesh pol
Ranch Hand
Posts: 151
Sorry kim
but i have now modified my method

public static int getNumberOfWeekEnds(String d1,String d2)throws Exception {
Date date1=getDate(d1);
Date date2=getDate(d2);

Calendar c1 = Calendar.getInstance();
c1.clear();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
c2.clear();
c2.setTime(date2);
int WeekEnds = 0;

long period=getDiffInDays(date1,date2);

if(d1.equals(d2)&&
(c1.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||
c1.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)){
System.out.println("two equal dates date1= "+d1+" date2= "+d2);
WeekEnds++;

}

if(!d1.equals(d2)){
System.out.println("two dates not equal date1= "+d1+" date2= "+d2);
while(c2.after(c1)) {

if(c1.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||
c1.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||
c2.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||
c2.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
WeekEnds++;
}
}
System.out.println(WeekEnds);
}
return WeekEnds;
}

i have made silly mistake in my code

Kenneth Albertson
Ranch Hand
Posts: 59
In the absence of the requested clarification about what the code is MEANT to do, I interpret the new version as indicating that it is TRYING to count the number of Saturdays AND Sundays (ie Weekend days) in any given period, including both of the two end dates. So I am assuming that these are the required results. I have added one extra test case, for reasons which will become obvious below.
It would be very helpful, Ganesh, if you could indicate whether or not this is what you are actually trying to do.

With your new code, I think that these are the answers that you will get: The lists of days after the counts are the actual days being counted as weekend days. So you can see that the code gets the right answer 6 times out of these 8 test cases, but only actually counts the correct days 4 out of 8 times.
[ October 10, 2005: Message edited by: Kym Thompson ]

ganesh pol
Ranch Hand
Posts: 151
Thanks kim for efforts now i think you will not get bug in my code

public static int getNumberOfWeekEnds(String d1,String d2)throws Exception {
// d1 is start date and d2 is end date
Date date1=getDate(d1);
Date date2=getDate(d2);

Calendar c1 = Calendar.getInstance();
c1.clear();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
c2.clear();
c2.setTime(date2);
int WeekEnds = 0;

long period=getDiffInDays(date1,date2);

if(d1.equals(d2)&&
(c1.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||
c1.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)){
System.out.println("two equal dates date1= "+d1+" date2= "+d2);
WeekEnds++;

}

if(!d1.equals(d2)){
System.out.println("two dates not equal date1= "+d1+" date2= "+d2);
// we have to check saturdays and sundays till leave end date(c2 date) //or// two dates

if((c1.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||
c1.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)||
(c2.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||
c2.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)){
while(/*c2.after(c1) && */!(c1.getTime().compareTo(c2.getTime())> 0)) {
System.out.println(" start day now "+c1.getTime());
if((c1.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||
c1.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)&&
(c2.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||
c2.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)){
WeekEnds++;
System.out.println(" week end now"+WeekEnds);
}
}//end of while

}else{
while(c2.after(c1) ) {
System.out.println(" start day now "+c1.getTime());
if((c1.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||
c1.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)||
(c2.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY||
c2.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)){
WeekEnds++;
System.out.println(" week end now"+WeekEnds);
}//end of if
}//end of while

}//end of else
System.out.println(WeekEnds);
}
return WeekEnds;

}
it is not the correct way to tell you, but if this code fails in Some way then tell me

Kenneth Albertson
Ranch Hand
Posts: 59
What results does this code produce for the test cases that I suggested? Especially the last one!

And please read this explanation of how to use code tages. You can't expect anyone to read code if it is not formatted.