1 2 Previous Next 16 Replies Latest reply: Mar 13, 2008 2:20 PM by Darryl Burke RSS

    date difference is not returing correct value due to leap year day ???

    807591
      I wrote a function to get the date difference in days

      please find the code below
        protected int getDateDiff(String date1, String date2)
          {
              SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yy");
              SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yy");
              long result = 0;
              long totalRange = 1000*3600*24;
              
              try
              {
                sdf1.setCalendar(Calendar.getInstance());
                sdf2.setCalendar(Calendar.getInstance());
      
                Date d1 = sdf1.parse(date1);
                Date d2 = sdf1.parse(date2);
                   long miliSeconds = d1.getTime()-d2.getTime();
                result = (miliSeconds/totalRange);
                 }
                 catch (Throwable t)
                    {
                      System.out.println("Error: " + t);
                    }
      
              return (int)result;
          }
      Some how this function didn't return the correct result between Feb 24 2008 to March 10 , 2008

      after that it started working , can anybody help me to findout what will be the issue with this function between those dates
        1 2 Previous Next