Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to calculate number of sundays and saturdays between two Dates

807603Oct 7 2005 — edited Nov 19 2007
friends i want to calculate how many Sundays come in two Dates
i have tried following code which is hard coded i have to impliment method which can give me number of Sundays between two Dates
please help me

import java.util.Date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Calendar ;
import java.util.GregorianCalendar;

public class DateDiffCalculator {
private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MMM-dd");

public DateDiffCalculator() {
}


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

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

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

}

public static int getYear(String date) {
//String[] day= {Sun,Mon,Tue,Wed,Thu,Fri,Sat};
Integer year=new Integer(date.substring(0,4));
return year.intValue();
}

public static int getMonth(String date) {
//String date.substring(5,7);
System.out.println(" "+date.substring(5,8));
String m= date.substring(5,8);
int month=0;
if(m.equalsIgnoreCase("Jan")) {
month=1;
}

if(m.equalsIgnoreCase("Feb")) {
month=2;
}

if(m.equalsIgnoreCase("Mar")) {
month=3;
}

if(m.equalsIgnoreCase("Apr")) {
month=4;
}

if(m.equalsIgnoreCase("May")) {
month=5;
}

if(m.equalsIgnoreCase("Jun")) {
month=6;
}

if(m.equalsIgnoreCase("Jul")) {
month=7;
}

if(m.equalsIgnoreCase("Aug")) {
month=8;
}

if(m.equalsIgnoreCase("Sep")) {
month=9;
}

if(m.equalsIgnoreCase("Oct")) {
month=10;
}

if(m.equalsIgnoreCase("Nov")) {
month=11;
}

if(m.equalsIgnoreCase("Dec")) {
month=12;
}

return month;
}

public static int getDay(String date) {
Integer day=new Integer(date.substring(9,11));
return day.intValue();

}

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);

// now get calender objects from it
GregorianCalendar c1= new GregorianCalendar(getYear(d1),getMonth(d1),getDay(d1));
GregorianCalendar c2= new GregorianCalendar(getYear(d2),getMonth(d2),getDay(d2));

// get period
long leavePeriod = getDiffInDays(date1,date2);



return 12; // it should return number of sundays but we type 12 to perform compilation
}

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





}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 17 2007
Added on Oct 7 2005
9 comments
1,937 views