如何在两个日期之间获得数月和数日

时间:2014-11-20 03:45:19

标签: java date elapsed

很抱歉,我已尝试为剩余的日期MonthsDays进行编码,遗憾的是我收到了错误的结果。任何帮助将不胜感激。谢谢!

SimpleDateFormat formatter= new SimpleDateFormat("dd-MM-yyyy");
String sdate    = "08-02-2016";
String edate     = "02-02-2017";   

Date startdate = formatter.parse(sdate);
Date enddate   = formatter.parse(eddate );

Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startdate);

Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(enddate);

int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR); //effdate -   currdate
int diffMonth = diffYear * 12 + endCalendar.get(Calendar.MONTH) -startCalendar.get(Calendar.MONTH); 
int diffDay= endCalendar.get(Calendar.DAY_OF_MONTH) -startCalendar.get(Calendar.DAY_OF_MONTH); 

预期结果:11个月,25天

P / s:JodaTime不适用。

2 个答案:

答案 0 :(得分:1)

我强烈建议您使用强大的API来获得可靠的结果,但如果您坚持手动执行,请尝试使用以下内容,它似乎给出了正确的结果(至少对于您的测试用例):

    SimpleDateFormat formatter= new SimpleDateFormat("dd-MM-yyyy");
    String CURRDATE    = "08-02-2016";
    String EFFDATE     = "02-02-2017";   

    Date startdate = formatter.parse(CURRDATE);
    Date enddate   = formatter.parse(EFFDATE);

    Calendar startCalendar = new GregorianCalendar();
    startCalendar.setTime(startdate);

    Calendar endCalendar = new GregorianCalendar();
    endCalendar.setTime(enddate);

    int monthCount = 0;
    int firstDayInFirstMonth = startCalendar.get(Calendar.DAY_OF_MONTH);
    startCalendar.set(Calendar.DAY_OF_MONTH, 1);
    endCalendar.add(Calendar.DAY_OF_YEAR, -firstDayInFirstMonth+1);

    while (!startCalendar.after(endCalendar)) {     
        startCalendar.add(Calendar.MONTH, 1);
        ++monthCount;
    }

    startCalendar.add(Calendar.MONTH, -1); --monthCount;
    int remainingDays = 0;
    while (!startCalendar.after(endCalendar)) {
        startCalendar.add(Calendar.DAY_OF_YEAR, 1);
        ++remainingDays;
    }

    startCalendar.add(Calendar.DAY_OF_YEAR, -1);
    --remainingDays;

    int lastMonthMaxDays = endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    if (remainingDays >= lastMonthMaxDays) {
        ++monthCount;
        remainingDays -= lastMonthMaxDays;
    }

    int diffMonth = monthCount; 
    int diffDay = remainingDays; 

    System.out.println("diffMonth==="+diffMonth +" Month(s) and " + diffDay + " Day(s)");

答案 1 :(得分:0)

我认为直接你不能像 z day y hr x min

那样

但是你可以获得单独的一天分钟,所有这些都在下面 -

    long diff = EFFDAT.getTime() - CURRDATE.getTime();
    long diffSeconds = diff / 1000 % 60;
    long diffMinutes = diff / (60 * 1000) % 60;
    long diffHours = diff / (60 * 60 * 1000);
    int diffInDays = (int) ((dt2.getTime() - dt1.getTime()) / (1000 * 60 * 60 * 24));

<强>已更新

希望它可以提供帮助 - 我认为你需要Java 8

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);

System.out.println("You are " + p.getYears() + " years, " + p.getMonths() + " months, and " + p.getDays() + " days old. (" + p2 + " days total)");

reference Oracle Page