get Number Of Days Between Dates - Android java.util

Android examples for java.util:Day

Description

get Number Of Days Between Dates

Demo Code

import java.util.Date;

public class Main{

  public static Date resetDateToBeginningOfDay(Date d) {
      d.setHours(0);//from   ww w.  j a v  a 2 s.  c  o m
      d.setMinutes(0);
      d.setSeconds(0);

      return d;
  }

  public static Date resetDateToEndOfDay(Date d) {
      d.setHours(23);
      d.setMinutes(59);
      d.setSeconds(59);

      return d;
  }
    public static int getNumberOfDaysBetweenDates(Date date1, Date date2) {
        Date d1 = (Date) date1.clone();
        Date d2 = (Date) date2.clone();

        d1 = resetDateToBeginningOfDay(d1);
        d2 = resetDateToEndOfDay(d2);

        return (int) Math
                .ceil(Math.abs((double) ((double) (d1.getTime() - d2
                        .getTime()) / 86400000d)));
    }

}

Related Tutorials