Java calculates a two-time difference in the number of days of the method rollup _java

Source: Internet
Author: User
Tags date1

Problem Description:

Input: two dates

Output: Two days of difference between dates

Specific code implementation

Method 1:

Compare by date of calendar class. Note: Here you need to consider:

    • Dates are trans-year, like one is 2012, one is 2015
    • Year is divided into years of peace year, the respective days are different
/**
 * Date2 more days than date1
 * @param date1 
 * @param date2 * @return * * 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)//the same year
 {
  int timedistance = 0;
  for (int i = year1 I < year2 i + +)
  {
  if (i%4==0 && i%100!=0 | | i%400==0)//Leap year  
  {
   Timedista NCE + + 366;
  }
  else//not leap year
  {
   timedistance + = 365;
  }
  }
  
  return timedistance + (day2-day1);
 }
 else//different years
 {
  System.out.println ("Judge Day2-day1:" + (Day2-day1));
  Return Day2-day1
 }
 }

Method 2:

By calculating the number of milliseconds for two dates, their difference is divided by the number of milliseconds in a day to get the number of days that we want for the two date difference.

/**
 * To determine the interval of two time by the number of milliseconds of time
 * @param date1
 * @param date2
 * @return/public
 static int Differentdaysbymillisecond (Date date1,date date2)
 {
 int for days = (int) (Date2.gettime ()-date1.gettime ())/( 1000*3600*24));
 return days;
 }

Test:

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 ("Two-date gap:" + differentdays (date,date2));
  System.out.println ("Two-date gap:" + differentdaysbymillisecond (date,date2));
 } catch (ParseException e) {
  e.printstacktrace ();
 }
 }

Results:

Two-date gap: 732
Two-date gap: 732
two ways to implement comparisons:

By the way, it is only through the date to compare the number of days of difference between two dates, there is no accurate to the difference between the time of day. If it is simply through the date (month-day) to compare the words is the way one.

For mode two, the number of days in the two-day difference is computed by calculating the number of milliseconds between two dates. One small problem is that when the difference is 23 hours, it's not a day. such as the following two dates

2015-1-1 21:21:28
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 ("Two-date gap:" + differentdays (date,date2));
  System.out.println ("Two-date gap:" + differentdaysbymillisecond (date,date2));
 } catch (ParseException e) {
  e.printstacktrace ();
 }
 }

Results:

Two-date gap: 1
Two-date gap: 0

The difference between the two ways, in the specific time difference of less than 24 hours, Mode 2 way is not a day, and the way 1 is through the date (month and year) to judge, so calculate the difference one day.

The above is the Java computation two time difference days method, and has carried on the detailed comparison, hoped for everybody's study to have the help.

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.