/ Published in: Java
URL: http://forum.java.sun.com/thread.jspa?threadID=447665&start=15&tstart=0
Expand |
Embed | Plain Text
// 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. // 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.... // Now we can print the day difference... Try it, it also works with // Feb 29...
You need to login to post a comment.
