两个日期之间的android天

时间:2014-10-10 13:30:51

标签: android datetime numbers days

我有两个日期时间09/01/2014和09/10 / 2014.i想要查看日期之间的日期。 我写了函数巫婆可以改变日期时间格式

public static String dateFormatterforLukka(String inputDate) {

    String inputFormat = "MM/dd/yyyy";
    String outputFormat = "d MMM";

    Date parsed = null;
    String outputDate = "";
    try {
        SimpleDateFormat df_input = new SimpleDateFormat(inputFormat,
                new Locale("en", "US"));
        SimpleDateFormat df_output = new SimpleDateFormat(outputFormat,
                new Locale("en", "US"));


        parsed = df_input.parse(inputDate);
        outputDate = df_output.format(parsed);
    } catch (Exception e) {
        outputDate = inputDate;
    }
    return outputDate;
}

但我不知道如何解决我的问题 如果有人知道解决方案,请帮助我

3 个答案:

答案 0 :(得分:0)

您可以在解析后以相同的日期格式解析它们,而不是使用2个简单格式,您将获得2个日期。您可以通过以下方式找到毫秒差异:

`

long differenceAsMs = higherDate.getTime() - lowerDate.getTime();
int differenceAsDay = differenceAsMs / (1000 * 60 * 60 * 24);

` 希望这可以帮助。

答案 1 :(得分:0)

您可以尝试此功能:

public String getDateDiffString(Date dateOne, Date dateTwo)
{
    long timeOne = dateOne.getTime();
    long timeTwo = dateTwo.getTime();
    long oneDay = 1000 * 60 * 60 * 24;
    long delta = (timeTwo - timeOne) / oneDay;

    if (delta > 0) {
        return "dateTwo is " + delta + " days after dateOne";
    }
    else {
        delta *= -1;
        return "dateTwo is " + delta + " days before dateOne";
    }
}

Courtesy Link

答案 2 :(得分:0)

使用joda时间库:JodaOrg/joda-time · GitHub

LocalDate date1 = new LocalDate(2014, 5, 25);
LocalDate date2 = new LocalDate(2014, 6, 15);

Period period = new Period(date1, date2, PeriodType.yearMonthDay());

int daysBetween = period.getDays();
int monthsBetween = period.getMonths();
int yearsBetween = period.getYears();

请注意,如果date2的日期早于date1的日期,则期间将返回负数,否则将返回正数。