Java calculates the difference between two dates in days and separated days

Source: Internet
Author: User

Java calculates the difference between two dates in days and separated days

Do you have any questions about the title of the article "days of difference between two dates and days of separation! It is understood from the text that there is a difference between "difference" and "separated". However, these differences have killed many people, but they have not found that in the case of a large number of new statistics, the difference is a thousand miles, I can find out that it was an accidental opportunity, but no one found that a project had been running for several years. I still had less than a year in it, at the beginning, the people who wrote the code did not know exactly what the situation was. No wonder the statistical data is always slightly different, the difference between a date and a date is sometimes equal and sometimes not equal. As you can see, I am also drunk when writing so many articles. Next, go to the topic.

 

 

Days of difference between two dates

 

We usually discuss the days of difference between two dates. A lot of code on the Internet is also about the days of difference between two dates, because the difference is already deep in the human mind, this code is also available in my managed projects, 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 and calculate the number of different days. The idea is to use the getTime () method of the java. util. Date class to obtain the number of milliseconds of the Date,

 

Then subtract dateEnd. getTime ()-dateStart. getTime (): returns the difference in milliseconds. divide it by 1000 to get the difference in seconds. divide it by 60 to get the difference in minutes. divide it by 60 to get the difference in hours; divide by 24 hours to get the difference in the number of days. This is the idea of calculating the number of days between two dates. Most of the Code on the internet is similar to this. The following focuses on the number of days between the next two dates.

 

 

Days between two dates

 

The separation is not a difference, as long as it is the same day, it is 0, not the same day, it is not 0, but for the days of the difference between the two dates is different, not the same day, the number of days may be 0.

Example of days difference,

Start Date: 23:59:59, end date: 00:00:00, difference is 1 second, difference days is 0,

The start date is 11:00:00, the end date is 10:59:59, the number of days is also 0, the difference is 1 second, the difference is 1.

 

Example of days separated,

Start Date: 23:59:59, end date: 00:00:00, the difference is 1 second, the days are 1,

The start date is 11:00:00, the end date is 10:59:59, the number of days is also 0, the difference is 1 second, the number of days is still 1.

By now, the readers should know the difference. If you use the idea of the number of days to find the number of days, it must be wrong.

 

The code for days is as follows:

 

The code for calculating the number of days is changed as long as it is changed to formatting and adding time:

 

By now, the code for calculating the number of days between two dates and the number of days separated has been completed. If the statistical log data is generated in different days and the results are different, the query results for millions of tables are very different. During development, you must carefully select the appropriate calculation method based on your business needs. It can only be said that details determine success or failure! If it is useful, I hope you can share it with us.

 

 

 

 


 

 

 

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.