membersound
membersound

Reputation: 86747

How to convert two dates difference into hours only?

I'd like to display the time difference between two dates in hours and minutes HH:mm. If the difference is more than 24 hours, the days/years difference should also only be displayed as total hours.

I have the following, but it might be incorrect for leap years for example. So I feel it is not the right way. I also have to take GMT time differences into account. How could I improve that:

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)

I'd prefer to achieve this without having to add an aditional library.

Upvotes: 2

Views: 408

Answers (4)

user2660000
user2660000

Reputation: 342

I would suggest to use Joda time library if you need to take in account leap years, time saving, etc. Have a look here http://www.joda.org/joda-time/

In Java 8 there is java.time package for this, have a look at the documentation here http://download.java.net/jdk8/docs/api/java/time/package-summary.html

Upvotes: 1

Masudul
Masudul

Reputation: 21961

Try to use TimeUnit.HOURS.convert method

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

Upvotes: 2

Chandranshu
Chandranshu

Reputation: 3669

I would suggest you use the excellent Joda Time library instead of the flawed java.util.Date and friends. You could simply write

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()
}

Upvotes: 1

constantlearner
constantlearner

Reputation: 5247

 // 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);

Upvotes: 0

Related Questions