9 Replies Latest reply: Oct 7, 2005 8:51 AM by 807603 RSS

    how to calculate  number of sundays and saturdays between two Dates

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





      }
      }
        • 2. Re: how to calculate  number of sundays and saturdays between two Dates
          807603
          Have you tried get(Calendar.DAY_OF_WEEK). This should give you the day of the week for a certain date.
          • 3. Re: how to calculate  number of sundays and saturdays between two Dates
            807603
            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
            • 4. Re: how to calculate  number of sundays and saturdays between two Dates
              807603
              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
              • 5. Re: how to calculate  number of sundays and saturdays between two Dates
                807603
                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.
                • 6. Re: how to calculate  number of sundays and saturdays between two Dates
                  807603
                  I need some time to give u the best result
                  • 7. Re: how to calculate  number of sundays and saturdays between two Dates
                    807603
                    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.
                    • 8. Re: how to calculate  number of sundays and saturdays between two Dates
                      807603
                      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.
                      • 9. Re: how to calculate  number of sundays and saturdays between two Dates
                        807603
                        *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
                        }