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

时间:2016-08-30 05:43:01

标签: android difference android-calendar android-date

我知道之前已经有过其他类似的问题,但就尝试过而来说,没有人创造出所需的输出,例如: 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);

,计算30.08.16和03.09.16之间的差值时输出如下

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

4 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:1)

  

Joda-Time为Java日期和时间提供了高质量的替代品   类。该设计允许多个日历系统,同时仍然   提供简单的API。

处理日期和时间很简单。

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

Joda Demo

答案 2 :(得分:1)

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;

答案 3 :(得分:1)

只需将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)));