Java method to calculate the number of days between two times

  • 2020-04-01 04:27:58
  • OfStack

Problem description:

Input: two dates

Output: the number of days between two dates

Specific code implementation

Method 1:

Date comparison through the Calendar class. Note: here's something to consider:

Dates span years, such as 2012 and 2015 Years are leap years and peace years, with different days


 public static int differentDays(Date date1,Date date2)
 {
 Calendar cal1 = Calendar.getInstance();
 cal1.setTime(date1);
 
 Calendar cal2 = Calendar.getInstance();
 cal2.setTime(date2);
 int day1= cal1.get(Calendar.DAY_OF_YEAR);
 int day2 = cal2.get(Calendar.DAY_OF_YEAR);
 
 int year1 = cal1.get(Calendar.YEAR);
 int year2 = cal2.get(Calendar.YEAR);
 if(year1 != year2) //In the same year
 {
  int timeDistance = 0 ;
  for(int i = year1 ; i < year2 ; i ++)
  {
  if(i%4==0 && i%100!=0 || i%400==0) //A leap year
  {
   timeDistance += 366;
  }
  else //Not a leap year
  {
   timeDistance += 365;
  }
  }
  
  return timeDistance + (day2-day1) ;
 }
 else //Different years
 {
  System.out.println(" judge day2 - day1 : " + (day2-day1));
  return day2-day1;
 }
 }

Method 2:

Simply by calculating the number of milliseconds between two dates, dividing their difference by the number of milliseconds of a day, we get the number of days between the two dates we want.


 
 public static int differentDaysByMillisecond(Date date1,Date date2)
 {
 int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
 return days;
 }

Testing:


public static void main(String[] args) 
 {
 String dateStr = "2008-1-1 1:21:28";
 String dateStr2 = "2010-1-2 1:21:28";
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 try 
 {
  Date date2 = format.parse(dateStr2);
  Date date = format.parse(dateStr);
  
  System.out.println(" The gap between the two dates: " + differentDays(date,date2));
  System.out.println(" The gap between the two dates: " + differentDaysByMillisecond(date,date2));
 } catch (ParseException e) {
  e.printStackTrace();
 }
 }

Results:

The difference between the two dates: 732
The difference between the two dates: 732
Comparison of two implementation methods:

The first way is to compare the number of days between two dates only by date, not to the exact time of day. If the comparison is purely by date (year, month, day) is the way one.

For mode two, the number of days between two dates is calculated by calculating the number of milliseconds between them. One small problem is that when they're 23 hours apart, it doesn't count as a day. The following two dates

The 2015-1-1 21:21:28
The 2015-1-2 1:21:28

Test code:


public static void main(String[] args) 
 {
 String dateStr = "2015-1-1 21:21:28";
 String dateStr2 = "2015-1-2 1:21:28";
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 try 
 {
  Date date2 = format.parse(dateStr2);
  Date date = format.parse(dateStr);
  
  System.out.println(" The gap between the two dates: " + differentDays(date,date2));
  System.out.println(" The gap between the two dates: " + differentDaysByMillisecond(date,date2));
 } catch (ParseException e) {
  e.printStackTrace();
 }
 }

Results:

The difference between the two dates: 1
The difference between the two dates: 0

The difference between the two ways is different, in the specific time difference of less than 24 hours, the way of way 2 is not a day, and the way of way 1 is judged by the date (year, month, day), so calculate a day.

The above is the Java method to calculate the difference between two days of time, and made a detailed comparison, I hope to help you learn.


Related articles: