Return to Snippet

Revision: 1366
at September 29, 2006 00:53 by pablazo


Initial Code
// First convert the from and to Calender to long (milli seconds)
// MAKE SURE THE Hour, Seconds and Milli seconds are set to 0, if you
// already have you own Claender object otherwise the time will be
// used in the comparision, later on.
long from = new java.util.GregorianCalendar(2003, 1, 20).getTime().getTime();
long to = new java.util.GregorianCalendar(2003, 2, 5).getTime().getTime();


// Next subtract the from date from the to date (make sure the
// result is a double, this is needed in case of Winter and Summer
// Time (changing the clock one hour ahead or back) the result will
// then be not exactly rounded on days. If you use long, this slighty
// different result will be lost.
double difference = to - from;


// Next divide the difference by the number of milliseconds in a day
// (1000 * 60 * 60 * 24). Next round the result, this is needed of the
// Summer and Winter time. If the period is 5 days and the change from
// Winter to Summer time is in the period the result will be
// 5.041666666666667 instead of 5 because of the extra hour. The
// same will happen from Winter to Summer time, the result will be
// 4.958333333333333 instead of 5 because of the missing hour. The
// round method will round both to 5 and everything is OKE....
long days = Math.round((difference/(1000*60*60*24)));


// Now we can print the day difference... Try it, it also works with
// Feb 29...
System.out.println(days);

Initial URL
http://forum.java.sun.com/thread.jspa?threadID=447665&start=15&tstart=0

Initial Description


Initial Title
Diferencia entre fechas JAVA

Initial Tags
date, java

Initial Language
Java