1. Achieve the goal
Input: two dates
Output: Two days with different dates
2. Code implementation
Method 1:
Date comparison through the Calendar class. Note: Here's what to consider:
- Dates are cross-year, such as one in 2012 and one in 2015
- Year is a leap year and a different number of days of peace years
/*** Date2 more days than Date1 *@paramDate1 *@paramDate2 *@return */ Public Static intdifferentdays (Date date1,date date2) {Calendar Cal1=calendar.getinstance (); Cal1.settime (date1);Calendar Cal2=calendar.getinstance (); Cal2.settime (DATE2); intday1=Cal1.get (calendar.day_of_year); intDay2 =Cal2.get (calendar.day_of_year); intYEAR1 =Cal1.get (calendar.year); intYEAR2 =Cal2.get (calendar.year); if(year1! = year2)//same year { inttimedistance = 0 ; for(inti = year1; i < year2; i + +) { if(i%4==0 && i%100!=0 | | i%400==0)//Leap Year{timedistance+ = 366; } Else //not a leap year{timedistance+ = 365; } } returnTimedistance + (day2-day1); } Else //different Years{System.out.println ("Judge Day2-day1:" + (day2-day1)); returnday2-Day1; } }
Method 2:
Directly by calculating the number of milliseconds of two dates, their difference divided by the number of milliseconds in a day, you can get the number of days we want to differ by the two date.
/** * Determine the interval of two time by the number of seconds in milliseconds @param date1 @param date2 @return */public static int Differentdaysbymillisecond (Date date1,date date2) { int days = (int) (( Date2.gettime ()-date1.gettime ())/(1000*3600*24)); return Days ; }
Test:
Public Static voidMain (string[] args) {String datestr= "2008-1-1 1:21:28"; String DATESTR2= "2010-1-2 1:21:28"; SimpleDateFormat format=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); SimpleDateFormat FORMAT2=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Try{Date Date2=Format.parse (DATESTR2); Date Date=Format.parse (DATESTR); System.out.println ("Two-day gap:" +differentdays (Date,date2)); System.out.println ("Two-day gap:" +Differentdaysbymillisecond (Date,date2)); } Catch(ParseException e) {e.printstacktrace (); } }
Results:
Two date gap: 732 Two-date gap:732
Comparison of two implementation methods:
Way one, just by the date to compare the difference between the two dates of the comparison of days, no accurate to a day of time. If it is purely through the date (year and month) to compare the comparison is the way one.
For mode two, the number of days difference of two dates is calculated by calculating the number of milliseconds that are different for two dates. There will be a small problem, that is, when they are 23 hours apart, it will not be counted for a day. As in the following two dates
2015-1-1 21:21:282015-1-2 1:21:28
Test code:
Public Static voidMain (string[] args) {String datestr= "2015-1-1 21:21:28"; String DATESTR2= "2015-1-2 1:21:28"; SimpleDateFormat format=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); SimpleDateFormat FORMAT2=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Try{Date Date2=Format.parse (DATESTR2); Date Date=Format.parse (DATESTR); System.out.println ("Two-day gap:" +differentdays (Date,date2)); System.out.println ("Two-day gap:" +Differentdaysbymillisecond (Date,date2)); } Catch(ParseException e) {e.printstacktrace (); } }
Results:
Two date gap: 1 Two date gap:0
Two different ways, in the specific time difference less than 24 hours, the way 2 is not a day, and the way 1 is through the date (month and day) to judge, so calculate the difference one day.
SOURCE Download: https://github.com/zcr1007391008/timeDifferent
Thanks: Thank you for reading!
Java determines the number of days between two time differences