0

Trying to make a little Countdown program in Java.

I am working on the finding the difference between the dates part of the app, and for some reason I am only getting the days.

I want the days, hours, minutes, and seconds. I am not sure why my calculation is just being rounded to an even integer... 163 in this example, and not 163.xx?

Here is my code:

import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class CalcData {

    Calendar cal1;
    Date today;

    public CalcData() {

        cal1 = new GregorianCalendar();
        today = new GregorianCalendar().getTime();

    }

    public String calcDiff() {

        cal1.set(2013, 6, 27, 7, 15, 0);
        double theDays = calcDays(cal1.getTime(), today);
        return "" + theDays;

    }

    public double calcDays(Date d1, Date d2) {

        double theCalcDays = (double) ((d1.getTime() - d2.getTime()) / (1000 * 60 * 60 * 24));
        return theCalcDays;

    }
}

When I run this, as I said, I get the result: 163.0

I would think, if it is using milliseconds, that it would be some sort of decimal. I am sure it is something simple that I am missing, but just can't find it!

Thanks in advance!

cbaze19
  • 5
  • 1
  • 3

1 Answers1

1

You are dividing an integer by another integer. You need to cast those to doubles before you do the division. Like this

double theCalcDays = ((double) (d1.getTime() - d2.getTime())) / (1000 * 60 * 60 * 24);
AgilePro
  • 5,588
  • 4
  • 33
  • 56
  • Sure enough! Thanks so much man.. Glad it was a simple mistake. – cbaze19 Feb 14 '13 at 07:39
  • This will not work, if both dates are from different daylight time saving zones. In such case, time difference will be X days-1 and 23 hours :) – Antoniossss May 06 '14 at 06:44
  • Indeed, in places that have daylight saving time, there is one day a year that is 23 hours long, and one day that is 25 hours long. However, inside the date object is a value in UTC. UTC has ONLY 24 hour days, and so it works. You always have this problem when you consider time zones: a two hour phone conference might start and end in the same day in one time zone, and it might start and end in different days in a different time zone. They disagree on the number of days! That has nothing to do with the duration, but instead the interpretation of the time according to that time zone. – AgilePro May 07 '14 at 00:06
  • You are correct, you CAN interpret difference of 23,9 days as 24 and the job would be done, hovewer, result itself is not correct, especially if you cast result to `int` – Antoniossss May 07 '14 at 05:33
  • Actually, he is not looking for an int. He is looking for a decimal with day and fractions of a day. -------- The concept of time zone, 25 hours days, and daylight simply make no sense if the original question is to come up with a decimal that represents days and fractions of days. I know you down-voted this answer because you know some things about dates, but if you visit the original question you will find there is no other reasonable interpretation. – AgilePro May 07 '14 at 05:45