Java calculates the difference between two dates and the number of days apart

Source: Internet
Author: User

You can see the article title "The difference between two days and the number of days apart", whether there are doubts about it! From the Chinese literal understanding, "difference" and "separation" is a difference, but these differences killed many people, but did not find, in a large number of new statistics is horseshoes great difference, I can all find is because of an accidental opportunity, a project run for several years but no one found, I was in it less than a year, At first, the people who wrote the code did not know what to do, and no wonder that the statistical data always had such subtle differences, that the date "difference" and "separation" were equal and sometimes unequal in certain circumstances. I was drunk when I wrote so many people. The following goes straight to the chase.


    • Two days of difference between dates

Usually we are discussing the difference between the two dates, the online lot of code is also about the difference between the two dates, because the difference in the human brain is already deep, in my management of the project also has such code, as shown below

public static int Getdiscrepantdays (date datestart, date dateend) {return (int) ((Dateend.gettime ()-Datestart.gettime () )/1000/60/60/24);}

The function of this method is to pass in the start and end dates, calculate the difference in days, the realization of the idea is to get the number of milliseconds by the Java.util.Date class GetTime () method,

then subtract Dateend.gettime ()-datestart.gettime (), get the difference in milliseconds, divide by 1000, get the difference of seconds, divide by 60, get the difference of minutes, divide by 60, get the difference of the number of hours, divide by 24 hours, get the difference of the number of days. This is the idea of counting the number of days between two dates. Most of the online code is similar to this one, the following focus on the next two days between the date.


    • Number of days between two dates

The distance is not a difference, as long as the same day, separated by 0, not the same date, not 0, but for two dates difference between the days is not the same, not the same, the number of days can also be 0.

Example of the difference in days,

The start date is: 2015-05-28 23:59:59, the end date is: 2015-05-29 00:00:00, the difference is 1 seconds, the difference of days is 0,

The start date is: 2015-05-28 11:00:00, the end date is: 2015-05-29 10:59:59, the difference of days also is 0, difference 1 seconds, the difference of days is 1.

Examples of days apart,

The start date is: 2015-05-28 23:59:59, the end date is: 2015-05-29 00:00:00, the difference is 1 seconds, the number of days is 1,

The start date is: 2015-05-28 11:00:00, the end date is: 2015-05-29 10:59:59, the difference of days is also 0, the difference is 1 seconds, the number of days or 1.

Here, the reader should know the difference is there, if the number of days between the idea of the number of days is bound to be wrong.


The code for days apart is as follows:



The number of days between the code as long as the adaptation, formatting and time, it becomes the calculation of the number of days apart code:



Here, calculate the number of days between two dates and the number of days to implement the code is finished. If the statistics of the log data generated in different days of the table two methods to obtain the results are decidedly different, for the millions of-level table information query, the results obtained by the difference is very large. All development should be based on their own business carefully choose the appropriate calculation method. Can only say the details determine success or failure! If useful, I hope you share it.









Java calculates the difference between two dates and the number of days apart

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.