JAVA comparison of two days difference between dates __java

Source: Internet
Author: User
Tags versions

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.