Sunday, August 26, 2012

Java : Getting no days between dates in java

private static double durationInDays(Date dateFrom, Date dateTo) {


TimeZone tz = Calendar.getInstance().getTimeZone();

/*TimeZone ltz = TimeZone.getTimeZone(tz.getID());

System.out.println(tz.getID());*/

double timeTo = dateTo.getTime();

double timeFrom = dateFrom.getTime();

double duration = timeTo - timeFrom;

if (tz.inDaylightTime(dateFrom)) {

if (!tz.inDaylightTime(dateTo)) {

System.out.println("in daylight saving from: " + dateFrom);

System.out.println("not in daylight saving to: " + dateTo);

duration -= 60 * 60 * 1000;

}

} else {

if (tz.inDaylightTime(dateTo)) {

System.out.println("not in daylight saving from: " + dateFrom);

System.out.println("in daylight saving to: " + dateTo);

duration += 60 * 60 * 1000;

}

}

return duration / (24 * 60 * 60 * 1000);

}