如何将两个日期差异转换为小时?

时间:2013-11-18 11:04:39

标签: java time

我想以小时和分钟HH:mm显示两个日期之间的时差。如果差异超过24小时,则天/年差异也应仅显示为总小时数。

我有以下内容,但例如闰年可能不正确。所以我觉得这不是正确的方法。我还必须考虑GMT时差。我怎么能改善这一点:

Calendar start; //eg 18.11.2013+05:00
Calendar end; //eg 20.11.2013-03:00

int years = end.get(Calendar.YEAR) - start.get(Calendar.YEAR);
int days = end.get(Calendar.DAY) - start.get(Calendar.DAY);
int hours = end.get(Calendar.HOUR) - start.get(Calendar.HOUR) + days * 24 + years * 365;
int minutes = end.get(Calendar.MINUTE) - start.get(Calendar.MINUTE);

//...format(HH:mm)

我更愿意在不添加附加库的情况下实现这一目标。

4 个答案:

答案 0 :(得分:2)

尝试使用TimeUnit.HOURS.convert方法

Calendar date1=...
Calendar date2=..
long hour= TimeUnit.HOURS.convert
          (date2.getTimeInMillis() -date1.getTimeInMillis(),
           TimeUnit.MILLISECONDS)

答案 1 :(得分:1)

我建议您使用优秀的Joda Time库而不是有缺陷的java.util.Date和朋友。你可以简单地写一下

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Duration;

long someMethod(){
    DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
    DateTime end = new DateTime(2005, 1, 1, 0, 0, 0, 0);
    Interval interval = new Interval(start, end);
    Duration duration = interval.toDuration(); //duration represents time in ms, from here do your conversions to any other unit.
    return duration.getStandardHours()
}

答案 2 :(得分:1)

如果您需要考虑闰年,节省时间等,我建议使用Joda时间库。看看http://www.joda.org/joda-time/

在Java 8中有java.time包,请看这里的文档 http://download.java.net/jdk8/docs/api/java/time/package-summary.html

答案 3 :(得分:0)

 // Create Calendar instance
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();

        // Set the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
        calendar1.set(2012, 2, 12);
        calendar1.set(2011, 3, 12);

        /*
         * Use getTimeInMillis() method to get the Calendar's time value in
         * milliseconds. This method returns the current time as UTC
         * milliseconds from the epoch
         */
        long miliSecondForDate1 = calendar1.getTimeInMillis();
        long miliSecondForDate2 = calendar2.getTimeInMillis();

        // Calculate the difference in millisecond between two dates
        long diffInMilis = miliSecondForDate2 - miliSecondForDate1;

        /*
         * Now we have difference between two date in form of millsecond we can
         * easily convert it Minute / Hour / Days by dividing the difference
         * with appropriate value. 1 Second : 1000 milisecond 1 Hour : 60 * 1000
         * millisecond 1 Day : 24 * 60 * 1000 milisecond
         */

        long diffInSecond = diffInMilis / 1000;
        long diffInMinute = diffInMilis / (60 * 1000);
        long diffInHour = diffInMilis / (60 * 60 * 1000);
        long diffInDays = diffInMilis / (24 * 60 * 60 * 1000);

        System.out.println("Difference in Seconds : " + diffInSecond);
        System.out.println("Difference in Minute : " + diffInMinute);
        System.out.println("Difference in Hours : " + diffInHour);
        System.out.println("Difference in Days : " + diffInDays);