Need to find diff between two dates : - 60 days I want to use Calendar. I could not figure out what is wrong with below code. ========================= //Date last address changed Calendar lastAddChnge = Calendar.getInstance(); lastAddChnge.clear();
lastAddChnge.setTime(lwa.getLastAddrChangeDate()); //Year int lastAddressChangedDayOfYear = lastAddChnge.DAY_OF_YEAR;
//Current Date Calendar sysCurrDate = Calendar.getInstance(); sysCurrDate.clear(); sysCurrDate.setTime(lwa.getCurrentSystemDate());
// Current Year int sysCurrDateDayOfYear = sysCurrDate.DAY_OF_YEAR;
What are you trying to do, look at the DAY_OF_YEAR value for both dates and comparing those? That will not work if one date is in 2005 and the other is in 2006, for example.
The DAY_OF_YEAR field returns the day number in the year. For example 20 December 2005 is day number 354, and 8 January 2006 is day number 8. If you subtract them you et 8 - 354 = -346. In other words, DAY_OF_YEAR is not suitable to find out the number of days difference between two dates.