In the test work is often used to compare the two date difference between days, I used to implement this feature of the two version of the method, the source code is as follows:
(version 1)
publicstaticint getintervaldays (date fdate, date odate) {
if (null = = FDate | | null = = odate) {
return-1;
}
long Intervalmilli = Odate.gettime ()-fdate.gettime ();
return (int) (Intervalmilli/(24 * 60 * 60 * 1000));
}
(version 2)
publicstaticint daysoftwo (date fdate, date odate) {
Calendar Acalendar = Calendar.getinstance ();
Acalendar.settime (fdate);
int day1 = Acalendar.get (calendar.day_of_year);
Acalendar.settime (odate);
int day2 = Acalendar.get (calendar.day_of_year);
return day2-day1;
}
Why use these two versions of the method, because I encountered in the process and I expected inconsistencies, such as the following example of a usage
publicstaticvoid Main (string[] args) {
Date fdate = new GregorianCalendar (). GetTime ();
Date odate = new GregorianCalendar (2009, 2, 15,13,13,0). GetTime ();
System.out.println ("Use getintervaldays difference days ="
+ getintervaldays (fdate, odate));
System.out.println ("Use daysoftwo difference days ="
+ Daysoftwo (fdate, odate));
} This example calls two versions of the method, but the output is sometimes different, before I have not understand why, and then carefully studied a bit, the original is my two version of the method is not clear understanding. Version 1 method to compare the strict sense of the day, for example, calculate today and yesterday compared to a few days, we may all think is 1, but we want to enter two date type parameters, this date contains not only days, hours, and so on, version 1 will be based on the hours you enter different, return different results, For example, you enter the parameters of this noon and last night two times, it will tell you a difference of 0 days. The same input version 2 only care about the day, the output is 1, is also the result I want. to sum up, both methods are right, and the key is that the caller understands what kind of results you want.