如何找到两个日期之间的差异持续时间

时间:2016-03-21 12:11:09

标签: java date

我有两个约会,例如。 1989-3-21,2016-3-21我希望找到这些日期之间的差异持续时间。为此,我尝试以下代码,但我无法获得日期差异的持续时间。

public String getTimeDiff(Date dateOne, Date dateTwo) {
        String diff = "";
        long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
        diff = String.format("%d hour(s) %d min(s)", TimeUnit.MILLISECONDS.toHours(timeDiff),
                TimeUnit.MILLISECONDS.toMinutes(timeDiff) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timeDiff)));
        return diff;
}

4 个答案:

答案 0 :(得分:2)

在致电public String getTimeDiff(Date dateOne, Date dateTwo)之前初始化您的日期:

    Date dateOne=null,dateTwo=null;
    try {
        dateOne = new SimpleDateFormat( "yyyy-MM-dd" ).parse("2016-3-21");
        dateTwo =  new SimpleDateFormat( "yyyy-MM-dd" ).parse("1989-3-21");
    } 
    catch (ParseException ex) {     
    }
    System.out.println( getTimeDiff(dateOne,dateTwo));



    public String getTimeDiff(Date dateOne, Date dateTwo) {
        String diff = "";
        long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
        diff = String.format("%d date(s) ", TimeUnit.MILLISECONDS.toDays(timeDiff));
        return diff;
    }

由于您的日期不是默认格式,因此您必须使用SimpleDateFormat明确声明日期的格式。

答案 1 :(得分:0)

来自here

    long diff = dt2.getTime() - dt1.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));

答案 2 :(得分:0)

尝试使用此

try {
            /// String CurrentDate=  "10/6/2016";
            /// String PrviousDate=  "10/7/2015";
            Date date1 = null;
            Date date2 = null;
            SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
            date1 = df.parse(CurrentDate);
            date2 = df.parse(PrviousDate);
            long diff = Math.abs(date1.getTime() - date2.getTime());
            long diffDays = diff / (24 * 60 * 60 * 1000);


            System.out.println(diffDays);

        } catch (Exception e) {
            System.out.println("exception " + e);
        }

答案 3 :(得分:0)

采取差异并调用方法;    long diff = dt2.getTime() - dt1.getTime();

public static String toHumanReadableTime(long diff) {
        Long hour = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
        diff= diff% (1000 * 60 * 60);
        Long minutes = TimeUnit.MINUTES.convert(diff, TimeUnit.MILLISECONDS);
        diff= diff% (1000 * 60);
        Long seconds = TimeUnit.SECONDS.convert(diff, TimeUnit.MILLISECONDS);
        diff= diff% 1000;
        Long milisec = diff;

        StringBuilder buffer = new StringBuilder();
        if (hour != null && hour > 0) {
            buffer.append(hour).append(" Hour ");
        }
        if (minutes != null && minutes > 0) {
            buffer.append(minutes).append(" Minute ");
        }
        buffer.append(seconds).append(" Second ");

        buffer.append(milisec).append(" Millisecond  ");

        return buffer.toString();
    }