Nilabja
Nilabja

Reputation: 4256

Calculate difference between two calender dates in days

I know there are other similar questions have been asked here before, but as far as tried them none created the desired output, e.g. difference between 02.09.2016 and 30.08.2016 should be 3 calendar days.

    long diff = oldDate.getTime() - currentDate.getTime();
    long seconds = diff / 1000;
    long minutes = seconds / 60;
    long hours = minutes / 60;
    long days = hours / 24;

    int year = (int) diff / 365;
    int rest = (int) diff % 365;
    int month = rest / 30;
    rest = rest % 30;
    int weeks = rest / 7;
    int dayss = rest % 7;

    long diffMillis= Math.abs(oldDate.getTime() - currentDate.getTime());
    long differenceInDays = TimeUnit.DAYS.convert(diffMillis, TimeUnit.MILLISECONDS);

    Log.d("ASDafldsfg", "" + days);
    Log.d("ASDafldsfg", "" + dayss);
    Log.d("ASDafldsfg", "" + differenceInDays);

and the output is as following while calculating difference between 30.08.16 and 03.09.16

D/ASDafldsfg: 3
D/ASDafldsfg: 6
D/ASDafldsfg: 3

Upvotes: 1

Views: 1393

Answers (4)

Nilabja
Nilabja

Reputation: 4256

just casting double to float and then applying Math.ceil() fixed the problem,

long diff = oldDate.getTime() - currentDate.getTime(); float days = (float) diff / 1000 / 60 / 60 / 24; tv_days.setText(String.valueOf((int) Math.ceil(days)));

Upvotes: 1

Kushan
Kushan

Reputation: 5984

JDK 1.8 introduced localDate and it has made life a lot easier for people... You can use it to calculate the difference between two dates as :

classes used :

import java.time.Month;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

LocalDate dateBefore=LocalDate.of( 2016,09,02); // you can also use Month.SEPTEMBER
LocalDate dateAfter=LocalDate.of(30,08,2016); //you can also use Month.AUGUST
long daysBetween = DAYS.between(dateBefore, dateAfter); // this is the chronounit for Days :)

The new time api is extremeley powerful, please do use it for convenience

Since android does not currently support all jdk 1.8 features, the above won't be helpful, use the following instead

date1 and date2 are normal Date objects obtained using calendar object initialized to your date and then Calendar.getTime()

long diff = date1.getTime() - date2.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;

Upvotes: 1

Sohail Zahid
Sohail Zahid

Reputation: 8149

Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API.

Its simple to handle with date and time.

Days.daysBetween(start.toLocalDate(), end.toLocalDate()).getDays()

Joda Demo

Upvotes: 1

Chirag Arora
Chirag Arora

Reputation: 816

Try this :

DateTimeUtils obj = new DateTimeUtils();
  SimpleDateFormat simpleDateFormat = 
            new SimpleDateFormat("dd/M/yyyy hh:mm:ss");

  try {

    Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10");
    Date date2 = simpleDateFormat.parse("13/10/2013 20:35:55");

    obj.printDifference(date1, date2);

  } catch (ParseException e) {
    e.printStackTrace();
  }

}

//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public void printDifference(Date startDate, Date endDate){

    //milliseconds
    long different = endDate.getTime() - startDate.getTime();

    System.out.println("startDate : " + startDate);
    System.out.println("endDate : "+ endDate);
    System.out.println("different : " + different);

    long secondsInMilli = 1000;
    long minutesInMilli = secondsInMilli * 60;
    long hoursInMilli = minutesInMilli * 60;
    long daysInMilli = hoursInMilli * 24;

    long elapsedDays = different / daysInMilli;
    different = different % daysInMilli;

    long elapsedHours = different / hoursInMilli;
    different = different % hoursInMilli;

    long elapsedMinutes = different / minutesInMilli;
    different = different % minutesInMilli;

    long elapsedSeconds = different / secondsInMilli;

    System.out.printf(
        "%d days, %d hours, %d minutes, %d seconds%n", 
        elapsedDays,
        elapsedHours, elapsedMinutes, elapsedSeconds);

}

Output is :

startDate : Thu Oct 10 11:30:10 SGT 2013
endDate : Sun Oct 13 20:35:55 SGT 2013
different : 291945000
3 days, 9 hours, 5 minutes, 45 seconds

Upvotes: 2

Related Questions