count days between dates - easy and very urgent

Hi, I just need code that counts the number of days between two dates which are in the following format:

String date1 = "dd-MM-yyyy";
String date2 = "dd-MM-yyyy";

Thank you.
iAwinAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
SimpleDateFormat sdf = new SimpleDateFormat("mm-MM-yyyy");
Date d1 = sdf.parse(date1);
Date d2 = sdf.parse(date2);
int days = d2.getTime() - d1.getTime())/1000/60/60/24;
0
 
armoghanCommented:
check this link as well
http://www.faqs.org/qa/qa-10170.html
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
armoghanCommented:
0
 
nesnemisCommented:
You can use this function...

private long countDays(String date1, String date2)
      {
            String date[] = date1.split("-");
            Calendar d1 = new GregorianCalendar(Integer.parseInt(date[2]), Integer.parseInt(date[1]), Integer.parseInt(date[0]));
            
            date = date2.split("-");
            Calendar d2 = new GregorianCalendar(Integer.parseInt(date[2]), Integer.parseInt(date[1]), Integer.parseInt(date[0]));
            
            long diff = d1.getTimeInMillis() - d2.getTimeInMillis();
            if(d1.before(d2))
                  diff = d2.getTimeInMillis() - d1.getTimeInMillis();
            
            return diff/(24*60*60*1000);
      }
0
 
iAwinAuthor Commented:

Thanks :)
0
All Courses

From novice to tech pro — start learning today.