Java determines the number of days between two time differences!

Source: Internet
Author: User
Tags add time date1 dateformat diff xpath

package com.datedaycha;    import java.text.simpledateformat;     import java.util.Calendar;    import java.util.Date;     import com.sun.org.apache.xerces.internal.impl.xpath.regex.parseexception;    / *     * java  determine the number of days between two time differences     1, achieve goals       Input: Two dates      outputs: Two days of difference     2, code implementation      Method 1:      date comparison by Calendar class. Note: It is important to consider that the:     date is a cross-year, such as a 2012, a 2015          Year is a leap year of peace years, the respective days are different      */    public class date_day_cha1  {    /** Method 1         *  Date2 more than date1 days          *  @param  date1             *  @param  date2          *  @return               */    public static int differentdays_one (Date &NBSP;DATE1,DATE&NBSP;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);     //Add time date size   judgment    tube Him who big who small   &Nbsp; int diff ;    if (day1<day2)  {    diff  = day2 - day1;    } else {    diff  = day1 - day2;    }    if (year1 !=  YEAR2)   //different years     {    int timedistance = 0  ;    for (int i = year1 ; i < year2 ;  i ++)     {    if (i%4==0 && i%100!=0  ||  i%400==0)     //leap year                  {    timedistance += 366;    }     else    //not leap year     {     Timedistance += 365;    }    }    //return timedistance +  (day2-day1)  ;    return timeDistance + diff ;     }    else     //same year     {     //            system.out.println (" Judge day2 - day1 :  " +  (day2-day1));    //             return day2-day1;            system.out.println ("Judging day2 - day1 : "  + diff );    return diff;    }    }     /* directly by calculating the number of milliseconds of two dates, dividing their difference by the number of milliseconds in a day, we can get the number of days that we want to differ by the two dates. */    /**             *  two-time interval by time-second-millisecond number               *  @param  date1              *  @param  date2              *  @return              */     public static int differentdaysbymillisecond (Date date1,Date  DATE2)     {           //Add a time date size   Judging    Tube He who big who small            long time1  = date1.gettime ();     long time2 = date2.gettime ();     long diff ;    if (time1<time2)  {    diff  = time2 - time1;    } else {    diff = time1 -  time2;    }    //int days =  (int)   (Date2.getTime ( )  - date1.gettime ())  /  (1000*3600*24));    int days =  ( int)   (diff /(1000*3600*24));    return days;    }     //Test     public static void main (String[] args)   throws exception {           string  datestr =  "2017-1-1 0:0:0";    string datestr2 =  "2017-9-14  10:57:01 ";     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   Method 1:"  +  Differentdays_one (Date,date2));     system.out.println ("Two-date gap   Method 2:"  +  Differentdaysbymillisecond (Date,date2));     date d=new date ();     system.out.println (D.gettime ());     system.out.println ("Distance today-two dates gap   Method 1:"  + differentdays_one (Date,d));     system.out.println ("Distance today-two dates gap   Method 2:"  + differentdaysbymillisecond (d,date));    } catch  (ParseException  e)  {    e.printstacktrace ();     }    }     }  Method 2: 
package com.datedaycha;    import java.text.dateformat;     import java.text.simpledateformat;    import java.util.date;     import com.sun.org.apache.xerces.internal.impl.xpath.regex.parseexception;    / **     *  time distance      *  @author  LC     *  @version  1.0     *  @date  2017-9-14 10:59:04     */     public class date_day_cha2 {     /**          *  two time difference between days            *  @param  one  Time Parameters  1:           *  @param  two  Time Parameters  2:           *  @return   Difference days           */       public static long getdistancedays (string str1, string  STR2)  throws Exception{    DateFormat df = new  SimpleDateFormat ("Yyyy-mm-dd");    date one;    date two;     long days=0;    try {    one =  df.parse (STR1);     two = df.parse (STR2);    long  Time1 = one.gettime ();     long time2 = two.gettime ();     long diff ;    if (time1<time2)  {    diff  = time2 - time1;    } else {    diff  = time1 - time2;    }    days = diff /  (1000 * 60  * 60 * 24);    } catch  (parseexception e)  {     e.printstacktrace ();    }    return days;     }    /**          *  Two time difference between how many days how many hours how many seconds           *  @param  str1  Time Parameters  1  Format: 1990-01-01 12:00:00          *   @param  str2  Time Parameters  2  format:2009-01-01 12:00:00           *  @return  long[]  return value: {days, , ,  seconds}           *  @throws  Exception           */&Nbsp;     public static long[] getdistancetimes (STRING&NBSP;STR1, &NBSP;STRING&NBSP;STR2)  throws exception {    dateformat df =  new simpledateformat ("Yyyy-mm-dd hh:mm:ss");    date one;     date two;    long day = 0;    long  hour = 0;    long min = 0;    long  sec = 0;    try {    one = df.parse ( STR1);     two = df.parse (str2);     long time1 =  one.gettime ();     long time2 = two.gettime ();     Long diff ;    if (time1<time2)  {    diff =  time2 - time1;    } else {    diff = time1 - time2;     }    day = diff /  (24 * 60 * 60 *  1000);    hour =  (diff /  (60 * 60 * 1000)  - day * 24);    min =  ((diff /  (60 *  ())  - day * 24 * 60 - hour * 60);     sec =  (diff/1000-day*24*60*60-hour*60*60-min*60);    } catch  ( parseexception e)  {    e.printstacktrace ();    }     long[] times = {day, hour, min, sec};    return  times;    }    /**           *  two X hoursDistance between the days how many hours how many minutes how many seconds           *  @param  str1  Time Parameters  1  Format: 1990-01-01 12:00:00          *   @param  str2  Time Parameters  2  format:2009-01-01 12:00:00           *  @return  String  return value: xx days xx hours xx minutes xx seconds            *  @throws  Exception           */      public static string getdistancetime (String  STR1,&NBSP;STRING&NBSP;STR2)  throws Exception {    DateFormat df  = new simpledateformat ("Yyyy-mm-dd hh:mm:ss");    date one;     Date two;    long day = 0;     Long hour = 0;    long min = 0;    long sec = 0;     try {    one = df.parse (STR1);     two  = df.parse (STR2);     long time1 = one.gettime ();     long time2 = two.gettime ();    long diff ;     if (time1<time2)  {    diff = time2 - time1;     } else {    diff = time1 - time2;     }    day = diff /  (24 * 60 *  60 * 1000);    hour =  (diff /  (60 * 60  * 1000)  - day * 24);    min =  ((diff /  (60  * 1000))  - dAY&NBSP;*&NBSP;24&NBSP;*&NBSP;60&NBSP;-&NBSP;HOUR&NBSP;*&NBSP;60);    sec =  ( diff/1000-day*24*60*60-hour*60*60-min*60);    } catch  (ParseException e)  {    e.printstacktrace ();     }    return  day +  "Days"  + hour +  "hours"  + min +  "Minutes"  + sec  +  "seconds";     }    //test     public  Static void main (String[] args)  throws Exception {        String dateStr =  "2008-1-1 1:21:28";    string  datestr2 =  "2017-9-14 10:57:01";     long days=getdistancedays (DATESTR, &NBSP;DATESTR2);     long[] longdays2;    longdays2= Getdistancetimes (DATESTR,&NBSP;DATESTR2);    string stringday3=  getdistancetime (DATESTR,&NBSP;DATESTR2);     system.out.println ("Return value returns: How many days difference:" +days);     system.out.println (" Return value return:long[]  return value: {Day, ,  minutes,  seconds}----: "+longdays2[0]+" Days "+longdays2[1]+" hours "+longdays2[2]+" +longdays2[3]+ "Seconds")     system.out.println ("Return value return:string  return value: xx days xx hours xx minutes xx seconds  -----: "+stringday3);     }    }


Java determines the number of days between two time differences!

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.