Search the Q&A Archives

# Calculating the Number of Days Between Two Dates in java

<< Back to: Java Programmers FAQ

 Question by neeraj Submitted on 12/30/2003 Related FAQ: Java Programmers FAQ Rating: Rate this question: N/A Worst Weak OK Good Great Calculating the Number of Days Between Two Dates in java

 Answer by Petru Chifor Submitted on 2/13/2004 Rating: Rate this answer: N/A Worst Weak OK Good Great GregorianCalendar da1 = new GregorianCalendar(2004,0,1,1,1,1);       GregorianCalendar da2 = new GregorianCalendar(2004,1,9,8,5,10);       long d1 = da1.getTime().getTime();       long d2 = da2.getTime().getTime();       long difMil = d2-d1;       long milPerDay = 1000*60*60*24;       long milPerOra = 1000*60*60;       long milPerMin = 1000*60;       long milPerSec = 1000;       long days = difMil / milPerDay;       long ore = ( difMil - days*milPerDay ) / milPerOra;       long min = ( difMil - days*milPerDay - ore*milPerOra ) / milPerMin;       long sec = ( difMil - days*milPerDay - ore*milPerOra - min*milPerMin ) / milPerSec;       long mil = ( difMil - days*milPerDay - ore*milPerOra - min*milPerMin - sec*milPerSec );       System.out.println("days="+days+" ore="+ore+" min="+min+" sec="+sec+" mil="+mil);

 Answer by DASTAS Submitted on 4/14/2005 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great My Answer for those situation: /**************************************************************************** * Author:  JAVIER A. DASTAS MENDEZ * NOTE  :   *  + Functions: *      no_days(initial_date, end_date) *        initial_date : STRING *        end_date     : STRING *        return  an int value (no of days) *      rest_of_days(end_date) *        end_date     : STRING *        return  an int value (no of days) *      StrToDate(string_date) *        string_date  : STRING *        return  DATE value (format "yyyy-MM-dd") ****************************************************************************/ import java.lang.*; import java.util.*; import java.text.*; public class days_elapse //  - A CLASS TO CALCULATE THE NUMBER //    OF DAYS BETWEEN TO DAYS //  - Convert a STRING to DATE //    with format "yyyy-MM-dd" {   private int no_of_days;      public days_elapse()   {   }      public int no_days(String f1, String f2)   //  Receive two strings with dates   //  to calculate the number of days   //  between the to dates   {     Date FechaActual = new Date();     GregorianCalendar tmpDate1 = new GregorianCalendar();     GregorianCalendar tmpDate2 = new GregorianCalendar();        int days_elapsed = 0;             tmpDate1.setTime(StrToDate(f1));         if (f2.equalsIgnoreCase("null"))  // check if the sencond date is nulls         {               tmpDate2.setTime(FechaActual);         }         else         {             tmpDate2.setTime(StrToDate(f2));                     }                                days_elapsed = calculate(tmpDate1, tmpDate2);     return days_elapsed;   }      private int calculate(GregorianCalendar t1, GregorianCalendar t2)   //  Calculate the number of days between TWO dates.   {     int ndays = 0;    int n;           if (t1.get(t1.YEAR) < t2.get(t2.YEAR))         {           ndays += (366 - t1.get(t1.DAY_OF_YEAR));                      for ( n = t2.get(t1.YEAR) + 1; n <= t2.get(t2.YEAR) - 1; n++)           {               ndays+=365;           }         }       ndays += t2.get(t2.DAY_OF_YEAR);              if (t2.get(t2.YEAR) == t1.get(t1.YEAR))       {         ndays =  t1.get(t1.DAY_OF_YEAR) - t2.get(t2.DAY_OF_YEAR);              }                      return ndays;   }        public int rest_of_days(String f2)   //  Calculate the number of days between the CURRENT DATE   //  and other STRING Date.   {         GregorianCalendar tmpDate1 = new GregorianCalendar();     GregorianCalendar tmpDate2 = new GregorianCalendar();        int days_elapsed = 0;         tmpDate1.setTime(new Date());          tmpDate2.setTime(StrToDate(f2));                      days_elapsed = calculate(tmpDate2, tmpDate1);     if (days_elapsed < 1)       days_elapsed = 0;            return days_elapsed;   }      private Date StrToDate(String f)   //  Convert a StRING to DATE,   //  the String format would be   //  "yyyy-MM-ddd" but not necessary   {     Date dFecha = new Date();       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");              try     {     dFecha = format.parse(f);     }     catch(ParseException e)     {       return dFecha;     }          return dFecha;   }      public double getNo_Days()   {       return no_of_days;   } }

 Answer by slaughters Submitted on 8/30/2005 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great Date OldDate = new Date("08/01/2005"); Date TodaysDate = new Date(); long mills_per_day = 1000 * 60 * 60 * 24; long day_diff = ( TodaysDate.getTime() - OldDate.getTime() ) / mills_per_day; out.print(day_diff);

 Answer by S W Submitted on 11/3/2005 Rating: Not yet rated Rate this answer: N/A Worst Weak OK Good Great Very great answer to my question. I have spent hours to learn a useful means for calculating the difference of two dates, however, no solid example shows me the full picture. Thanks to Petru Chifor, you solved my problem instantly!

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.

 Your name or nickname: If you'd like to create a new account or access your existing account, put in your password here: Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

<< Back to: Java Programmers FAQ