月,日的日期差异

时间:2016-11-26 10:21:22

标签: android date datetime

实际上我遇到了一些小问题,但没有得到任何适当的解决方案。我有两个相同格式的日期现在我想要区别但格式将是(年 - 月 - 天 - 小时 - 薄荷 - 秒),这是我的代码

  public static void getDifferent(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 monthInMili = daysInMilli * 30;

        long elapsedMonths = different / monthInMili;

        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;

        Log.d("Difference", "startDate: " + startDate.toString() + " endDate: " + endDate + " Months : " + elapsedMonths + " Days :" + elapsedDays + " Hours :" +
                elapsedHours + " Mint :" + elapsedMinutes + " Seconds :" + elapsedSeconds);


    }

由于这一行,这段代码并不完美

long monthInMili = daysInMilli * 30;

所以请指导我。我很长时间没有得到适当的解决方案。

2 个答案:

答案 0 :(得分:3)

修改:要获得月份差异,您可以使用Calender类。否则,请使用JodaTime库(在答案底部链接)。

Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startDate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);

int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int diffMonth = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
int diffDay = endCalendar.get(Calendar.DAY_OF_MONTH) - startCalendar.get(Calendar.DAY_OF_MONTH);

请注意,如果您的日期是2013-01-31和2013-02-01,那么您可以通过这种方式获得1个月的距离,这可能是您想要的,也可能不是。要更正此问题,请参阅this answer

编辑前

只需减去日期并根据需要计算毫秒数

// Start Date : 01/14/2012 09:29:58
// End Date   : 01/15/2012 10:31:48

public static void getDifference(Date d1, Date d2){

    // HH converts hour in 24 hours format (0-23), day calculation
    // must match with your date format
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    try {

        //in milliseconds
        long diff = d2.getTime() - d1.getTime();

        long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);

        System.out.print(diffDays + " days, ");
        System.out.print(diffHours + " hours, ");
        System.out.print(diffMinutes + " minutes, ");
        System.out.print(diffSeconds + " seconds.");

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

}

您也可以使用joda-time-library并按照此tutorial's second step进行实施。

答案 1 :(得分:1)

  

你必须写这样的代码

    long different = todayDate.getTime() - meetingDate.getTime();

    long secondsInMilli = 1000;
    long minutesInMilli = secondsInMilli * 60;
    long hoursInMilli = minutesInMilli * 60;
    long daysInMilli = hoursInMilli * 24;
    long mothsInMilli = daysInMilli * 30;
    long yearInMilli = mothsInMilli * 12;

    long elapsedYear = different / yearInMilli;
    different = different % yearInMilli;
    System.out.println("--------- elapsedYear : " + elapsedYear);

    long elapsedMonths = different / mothsInMilli;
    different = different % mothsInMilli;
    System.out.println("--------- elapsedMonths : " + elapsedMonths);

    long elapsedDays = different / daysInMilli;
    different = different % daysInMilli;
    System.out.println("--------- elapsedDays : " + elapsedDays);

    long elapsedHours = different / hoursInMilli;
    different = different % hoursInMilli;
    System.out.println("--------- elapsedHours : " + elapsedHours);

    long elapsedMinutes = different / minutesInMilli;
    different = different % minutesInMilli;
    System.out.println("--------- elapsedMinutes : " + elapsedMinutes);

    long elapsedSeconds = different / secondsInMilli;
    System.out.println("--------- elapsedSeconds : " + elapsedSeconds);