6 Replies Latest reply: Mar 5, 2007 8:01 AM by 807606 RSS

    difference between two dates excluding sat/sun

    807606
      please help me if you know how to find out the number of dates between two dates excluding saturday and sunday . If any body knows it please reply .
        • 1. Re: difference between two dates excluding sat/sun
          807606
          Hello,

          Check the java.util.Calendar.

          There is also the new JSR 310 which is the Date Time API but, not yet available for download, however, it's based on Joda Time
          which is here http://joda-time.sourceforge.net/

          Regards,
          Sebastien Degardin.
          • 2. Re: difference between two dates excluding sat/sun
            807606
            You can try this
            import java.util.Date;
            import java.util.Locale;
            import java.util.Calendar;
            import java.text.DateFormat;
            
            public class Test {
            
                 public static void main(String[] args) {
                      Date date1 = null;
                      Date date2 = null;
                      DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
            
                      try {
                           date1 = df.parse("05 Mars 2007");
                           date2 = df.parse("26 Mars 2007");
                           System.out.println(date1);
                           System.out.println(date2);
                           System.out.println(diffDates(date1, date2));
                      } catch (Exception e) {
                           e.printStackTrace();
                      }
                 }
            
                 // date2 must be higher to date1
                 private static int diffDates(Date date1, Date date2) {
                      int result = 0;
                      Calendar midDate = Calendar.getInstance(); // Instance for the working date
                      Calendar endDate = Calendar.getInstance(); // Instance for the latest date
                      midDate.setTime(date1); // Init with date1
                      endDate.setTime(date2); // Init with date2
                      endDate.add(Calendar.DATE,1); // Needed to test the date2 value else it will stop at date2-1
                      while (midDate.before(endDate)) {
                           if (midDate.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && midDate.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) result++;
                           midDate.add(Calendar.DATE,1);
                      }
                      return result;
                 }
            }
            • 3. Re: difference between two dates excluding sat/sun
              807606
              THANKS A LOT Sebastien...
              • 4. Re: difference between two dates excluding sat/sun
                807606
                THANKS A LOT ..THIS HELPED ME OUT ..THANKS
                • 5. Re: difference between two dates excluding sat/sun
                  807606
                  Your welcome ;)
                  • 6. Re: difference between two dates excluding sat/sun
                    807606
                    If a thanks is for me, your welcome :)

                    don't forget to distrib the dollars ;)