# Posted By

pablazo on 09/29/06

# Statistics

Viewed 94 times
Favorited by 0 user(s)

# Diferencia entre fechas JAVA

/ Published in: Java
`// 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);`