计算两个日历日期之间的天数差异

Calculate difference between two calender dates in days

我知道之前这里也问过其他类似的问题,但据我所试 none 创建了所需的输出,例如2016 年 9 月 2 日和 2016 年 8 月 30 日之间的差异应为 3 个日历日。

    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);

计算 2016 年 8 月 30 日和 16 年 9 月 3 日之间的差异时输出如下

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

试试这个:

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);

}

输出为:

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

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.

处理日期和时间很简单。

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

Joda Demo

JDK 1.8 引入了 localDate,它让人们的生活变得更加轻松......您可以使用它来计算两个日期之间的差异:

类 使用过:

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 :)

新时间api非常强大,请大家使用方便

由于 android 目前不支持所有 jdk 1.8 功能,以上内容无济于事,请改用以下内容

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;

只需将 double 转换为 float,然后应用 Math.ceil() 即可解决问题,

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