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?
- Long beginTime = Begindate.gettime ();
- Long endTime = Enddate.gettime ();
- Long betweendays = (long) ((Endtime-begintime)/(+ * 24));
Here are two special cases to test:
Scenario 1:
[Java]View Plaincopy print?
- Calendar Begincalendar = Calendar.getinstance ();
- Begincalendar.set (2,3,1,0,0); //Set time is March 3, 2017 1:0:0
- Calendar Endcalendar = Calendar.getinstance ();
- Endcalendar.set (2,3, 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)/(+ * 24));
- 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?
- Calendar Begincalendar = Calendar.getinstance ();
- Begincalendar.set (2,2, +),<span style="WHITE-SPACE:PRE;" > </span>/Set Time is March 2, 2017 20:20:20
- Calendar Endcalendar = Calendar.getinstance ();
- Endcalendar.set (2,3,Ten,10); //Set time is March 3, 2017 10:10:10
- Long beginTime = Begincalendar.gettime (). GetTime ();
- Long endTime = Endcalendar.gettime (). GetTime ();
- Long betweendays = (long) ((Endtime-begintime)/(+ * 24));
- 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?
- 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)/(+ * * )); The difference between two milliseconds is greater than the number of days in a day
- 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
- Endcalendar.add (Calendar.day_of_month,-1); and subtract 1 days from the Endcalendar .
- if (Begincalendar.get (calendar.day_of_month) ==endcalendar.get (calendar.day_of_month))//Compare two dates of day_of_ Is month equal
- return betweendays + 1; //Equal description Indeed cross the sky
- Else
- return betweendays + 0; //Unequal description does not cross day
- }
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?
- 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