participate


Java Programming - how to calculate number of sundays and saturdays between two Dates
<<   Back to Forum  |   Give us Feedback
This topic has 9 replies on 1 page.
ganesh_pol
Posts:12
Registered: 6/21/05
how to calculate number of sundays and saturdays between two Dates   
Oct 7, 2005 4:56 AM

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





}
}
 
yawmark
Posts:30,119
Registered: 2/3/03
Re: how to calculate number of sundays and saturdays between two Dates   
Oct 7, 2005 5:00 AM (reply 1 of 9)  (In reply to original post )

 
[url=http://www.javaworld.com/jw-12-2000/jw-1229-dates.html]Calculating Java dates: Take the time to learn how to create and use dates[/url]
[url=http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html]Working in Java time: Learn the basics of calculating elapsed time in Java[/url]
 
.....uj
Posts:61
Registered: 10/4/05
Re: how to calculate number of sundays and saturdays between two Dates   
Oct 7, 2005 5:16 AM (reply 2 of 9)  (In reply to original post )

 
Have you tried get(Calendar.DAY_OF_WEEK). This should give you the day of the week for a certain date.
 
ganesh_pol
Posts:12
Registered: 6/21/05
Re: how to calculate number of sundays and saturdays between two Dates   
Oct 7, 2005 5:19 AM (reply 3 of 9)  (In reply to #1 )

 
thanks now i have modified the get Month Code
as follows

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=0;
}

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

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

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

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

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

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

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

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

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

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

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

return month;
}
but question remains same how to calculate number of Sundays Between 2 Dates
 
ganesh_pol
Posts:12
Registered: 6/21/05
Re: how to calculate number of sundays and saturdays between two Dates   
Oct 7, 2005 6:51 AM (reply 4 of 9)  (In reply to original post )

 
i have modified code as follows

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 getNumberofSundays(String d1,String d2) throws Exception {

// 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;
}

when i divide leavePeriod by 7 it gives me no of weeks no of weeks is equal to no of sundays
but how to handle than flot value
 
.....uj
Posts:61
Registered: 10/4/05
Re: how to calculate number of sundays and saturdays between two Dates   
Oct 7, 2005 8:35 AM (reply 5 of 9)  (In reply to #3 )

 
but question remains same how to calculate number of
Sundays Between 2 Dates

It shouldn't be that hard if you have the weekday of the starting date and the total number of days.
 
sun_ashishpandey
Posts:1
Registered: 7/24/07
Re: how to calculate number of sundays and saturdays between two Dates   
Jul 24, 2007 11:08 PM (reply 6 of 9)  (In reply to #2 )

 
I need some time to give u the best result
 
cotton.m
Posts:15,338
Registered: 8/26/05
Re: how to calculate number of sundays and saturdays between two Dates   
Jul 24, 2007 11:28 PM (reply 7 of 9)  (In reply to #6 )

 
I need some time to give u the best result

This thread is two years old. Please in future check that your brain is securely fastened before entering the forum.
 
flounder
Posts:14,606
Registered: 3/2/05
Re: how to calculate number of sundays and saturdays between two Dates   
Jul 24, 2007 11:29 PM (reply 8 of 9)  (In reply to #6 )

 
I need some time to give u the best result

Any more time and they will be fucking dead before you give them a solution.
 
rathnakar
Posts:6
Registered: 5/23/07
Re: how to calculate number of sundays and saturdays between two Dates   
Nov 19, 2007 4:14 AM (reply 9 of 9)  (In reply to #8 )

 
*i changed getNumberofSundays Method. I think it is use ful for you*
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),getDay1(d1));
GregorianCalendar c2= new GregorianCalendar(getYear(d2),getMonth(d2),getDay1(d2));
System.out.println("DAY_OF_MONTH: " c1.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: "
c1.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " c1.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_MONTH: "
c2.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " c2.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: "
c2.get(Calendar.DAY_OF_WEEK));
int noofsun=0;

long leavePeriod = Math.abs(getDiffInDays(date1,date2));
System.out.println("total No of Days"leavePeriod);
float noOfWeeks=leavePeriod/7;
noofsun=(int)noOfWeeks;
if(c1.get(Calendar.DAY_OF_WEEK)+leavePeriod%7>=7)
noofsun=noofsun+1;

//System.out.print(date2.getDay());
//System.out.println("no of weeks"+noOfWeeks
" remainder +remainder"+leavePeriod%7);
//System.out.println("total sun"+noofsun);
return noofsun;

// it should return number of sundays but we type 12 to perform compilation
}
 
This topic has 9 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums