Java: Correctly calculates the interval between two dates through the Calendar class

Source: Internet
Author: User
Tags set time

In the development of Android applications occasionally need to use a hint that the user has used the number of days of the function, from the implementation of this is to persist in the user's first time to use the application firsttime(through sharedpreferences, XML, SQLite, etc.), when the user uses the app again to get the time Presenttime, two time to calculate its interval number of days.

When two time variables are obtained, the practice of calculating date intervals on the net is usually the case (getting the difference in milliseconds for two times before processing):

[Java]View Plaincopy print?
    1. Long beginTime = Begindate.gettime ();
    2. Long endTime = Enddate.gettime ();
    3. Long betweendays = (long) ((Endtime-begintime)/(+ * 24));

Here are two special cases to test:

Scenario 1:

[Java]View Plaincopy print?
  1. Calendar Begincalendar = Calendar.getinstance ();
  2. Begincalendar.set (2,3,1,0,0); //Set time is March 3, 2017 1:0:0
  3. Calendar Endcalendar = Calendar.getinstance ();
  4. Endcalendar.set (2,3, 0,0); //Set time is March 3, 2017 14:0:0
  5. Long beginTime = Begincalendar.gettime (). GetTime ();
  6. Long endTime = Endcalendar.gettime (). GetTime ();
  7. Long betweendays = (long) ((Endtime-begintime)/(+ * 24));
  8. System.out.println (betweendays); //output is 0
Calendar Begincalendar = Calendar.getinstance (); Begincalendar.set (2017,2,3,1,0,0);//Set Time is March 3, 2017 1:0:0 Calendar Endcalendar = Calendar.getinstance (); Endcalendar.set (2017,2,3,14,0,0);//Set Time is March 3, 2017 14:0:0 long beginTime = Begincalendar.gettime (). GetTime (); Long endTime = Endcalendar.gettime (). GetTime (); Long betweendays = (long) ((Endtime-begintime)/(1000 * 60 * 60 *24)); System.out.println (betweendays);//output is 0

Scenario 2:

[Java]View Plaincopy print?
  1. Calendar Begincalendar = Calendar.getinstance ();
  2. Begincalendar.set (2,2, +),<span style="WHITE-SPACE:PRE;" > </span>/Set Time is March 2, 2017 20:20:20
  3. Calendar Endcalendar = Calendar.getinstance ();
  4. Endcalendar.set (2,3,Ten,10); //Set time is March 3, 2017 10:10:10
  5. Long beginTime = Begincalendar.gettime (). GetTime ();
  6. Long endTime = Endcalendar.gettime (). GetTime ();
  7. Long betweendays = (long) ((Endtime-begintime)/(+ * 24));
  8. System.out.println (betweendays); //output is 0, but should actually be 1
Calendar Begincalendar = Calendar.getinstance (); Begincalendar.set (2017,2,2,20,20,20); //Set time is March 2, 2017 20:20:20  Calendar Endcalendar = Calendar.getinstance (); Endcalendar.set (2017,2,3,10,10,10);//Set Time is March 3, 2017 10:10:10long BeginTime = Begincalendar.gettime (). GetTime (); Long endTime = Endcalendar.gettime (). GetTime (); Long betweendays = (long) ((Endtime-begintime)/(1000 * 60 * 60 *24)); System.out.println (betweendays);//output is 0, but should actually be 1

It can be seen that the code output is 0 for cases where the time interval is 1. The reason for this is that, when the millisecond of the two date is the difference < The number of milliseconds in a day (*60*60*24) , the result becomes 0 even if the two dates span one day, forcing the type to convert. You can see two cases where the difference between milliseconds is less than one day.

Case 1 (The difference in milliseconds is less than one day and not across days, the date interval should be 0):

Case 2 (the difference in milliseconds is less than one day and across days, the date interval should be 1):

So the code above needs to be improved so that it can recognize this special situation across days:

[Java]View Plaincopy print?
  1. Public static int gettimedistance (date begindate, date endDate) {
  2. Calendar Begincalendar = Calendar.getinstance ();
  3. Begincalendar.settime (begindate);
  4. Calendar Endcalendar = Calendar.getinstance ();
  5. Endcalendar.settime (endDate);
  6. Long beginTime = Begincalendar.gettime (). GetTime ();
  7. Long endTime = Endcalendar.gettime (). GetTime ();
  8. int betweendays = (int) ((endtime-begintime)/(+ * * )); The difference between two milliseconds is greater than the number of days in a day
  9. Endcalendar.add (Calendar.day_of_month,-betweendays); //Make Endcalendar subtract these days, convert the problem to a two-time millisecond difference less than one day
  10. Endcalendar.add (Calendar.day_of_month,-1); and subtract 1 days from the Endcalendar .
  11. if (Begincalendar.get (calendar.day_of_month) ==endcalendar.get (calendar.day_of_month))//Compare two dates of day_of_ Is month equal
  12. return betweendays + 1; //Equal description Indeed cross the sky
  13. Else
  14. return betweendays + 0; //Unequal description does not cross day
  15. }
public static int gettimedistance (date begindate, date endDate) {    Calendar Begincalendar = Calendar.getinstance (); 
   begincalendar.settime (begindate);    Calendar Endcalendar = Calendar.getinstance ();    Endcalendar.settime (endDate);    Long beginTime = Begincalendar.gettime (). GetTime ();    Long endTime = Endcalendar.gettime (). GetTime ();    int betweendays = (int) ((endtime-begintime)/(1000 * 60 * 60 *24));//The difference in milliseconds between two times is greater than the number of days in a day        Endcalendar.add (CALENDAR.D Ay_of_month,-betweendays);//causes Endcalendar to subtract these days, converting the problem to a two-time millisecond difference of less than one day    Endcalendar.add (calendar.day_of_ MONTH,-1);//Endcalendar minus 1 days    if (Begincalendar.get (calendar.day_of_month) ==endcalendar.get (calendar.day_of _month)//Compare the day_of_month of two dates to        return betweendays + 1;//equal description Indeed cross the day    else        return betweendays + 0;// Unequal descriptions do not cross days}

There is a detail in the code that needs to be noted, I first make endcalendar.add (Calendar.day_of_month,-1), and then compare the day_of_month time values of the two dates for equality, thus determining the return value

And not directly

[Java]View Plaincopy print?
    1. return betweendays + endcalendar.get (calendar.day_of_month)-begincalendar.get (calendar.day_of_month);
return betweendays + endcalendar.get (calendar.day_of_month)-begincalendar.get (calendar.day_of_month);

is due to the possibility of a cross-month situation, such as Begincalendar's date is February 28, Endcalendar date March 1, Endcalendar.get (calendar.day_of_month)- The value of Begincalendar.get (Calendar.day_of_month) will be 27 instead of the expected 1. Using Endcalendar.add (Calendar.day_of_month,-1), the Calendar class method automatically transforms the cross-month situation.

Java: Correctly calculates the interval between two dates through the Calendar class

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.