Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , ,

Sponsored Links



Program to calculate days difference between two dates in java.

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 20-01-2010
Member
 
Join Date: Aug 2009
Posts: 57
Program to calculate days difference between two dates in java.

Hello friends,
I am last year B.Sc.I.T. student. I recently started learning java language. In our last lecture our sir has given one program like "Write a Program to calculate days difference between two dates in java", but none of us able to write right program. That's why I am asking this question on this forum. Any one know how to write Program to calculate days difference between two dates in java. Please help me.
Thanks in advanced.
Reply With Quote
  #2  
Old 20-01-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,238
Re: Program to calculate days difference between two dates in java.

In the following example we are going to calculate the differences between two dates. For this you have to first converts the actual date value into the corresponding date / time value in milliseconds and then do the math so we can get the different in days, hours, minutes, etc.



Code:
package org.kodejava.example.util;
 
import java.util.Calendar;
 
public class DD
{   
    public static void main(String[] args)
    {
       
       Calendar c1 = Calendar.getInstance();
       Calendar c2 = Calendar.getInstance();
         
           c1.set(2006, 12, 30);
        c2.set(2007, 5, 3);
 
        long milis1 = c1.getTimeInMillis();
        long milis2 = c2.getTimeInMillis();

        long d = milis2 - milis1;
         
        long dSeconds = diff / 1000;
         
 
        long dM = d / (60 * 1000);
       
        long dH = d / (60 * 60 * 1000);

        long dD = d / (24 * 60 * 60 * 1000);
         
        System.out.println("In milliseconds: " + d + " milliseconds.");
        System.out.println("In seconds: " + dS + " seconds.");
        System.out.println("In minutes: " + dM + " minutes.");
        System.out.println("In hours: " + dH + " hours.");
        System.out.println("In days: " + dD + " days.");
    }
}
Reply With Quote
  #3  
Old 20-01-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
Re: Program to calculate days difference between two dates in java.

You have to use setTimeInMillis(long millis) method to set current time in calendar object and you have to use getInstance() method to get a calendar using the default time zone, locale and current time.

Code:
import java.util.Calendar;
 
public class DateDifferent{    
    public static void main(String[] args){
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    c1.set(2010, 01, 21);
    ca2.set(2010, 10, 21);
    long m1 = calendar1.getTimeInMillis();
    long m2 = calendar2.getTimeInMillis();
    long d = m2 - m1;
    long dS = d / 1000;
    long dM = d / (60 * 1000);
    long dH = d / (60 * 60 * 1000);
    long dD = d / (24 * 60 * 60 * 1000);
    System.out.println("\nThe Date Different Example");
    System.out.println("Time in milliseconds: " + d
 + " m.");
    System.out.println("Time in seconds: " + dS
 + " seconds.");
    System.out.println("Time in minutes: " + dM 
+ " minutes.");
    System.out.println("Time in hours: " + dH
+ " hours.");
    System.out.println("Time in days: " + dD 
+ " days.");
  }
}
Reply With Quote
  #4  
Old 20-01-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
Re: Program to calculate days difference between two dates in java.

Hey it very easy process to calculate days difference between two days. I had written one example of it. Just go through it.

Code:
import java.util.Date;
import java.util.GregorianCalendar;


public class DateDifference {
  public static void main(String[] av) {
    
    Date da1 = new GregorianCalendar(2000, 11, 31, 23, 59).getTime();


    Date today1 = new Date();

 
    long diff1 = today1.getTime() - da1.getTime();

    System.out.println("The year (up to " + today + ") is "
        + (diff1 / (1000 * 60 * 60 * 24)) + "  the days old.");
  }
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 20-01-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
Re: Program to calculate days difference between two dates in java.

Hey you have to use following code in your program to calculate days difference between two days.

Code:
   
 public static long daysBetween(Calendar sDate, Calendar eDate) {  
   Calendar d = (Calendar) sDate.clone();  
  long dBetween = 0;  
   while (d.before(eDate)) {  
     d.add(Calendar.DAY_OF_MONTH, 1);  
     dBetween++;  
   }  
   return dBetween;  
 }  
  12. }
Reply With Quote
  #6  
Old 16-03-2011
Member
 
Join Date: Mar 2011
Posts: 1
Re: Program to calculate days difference between two dates in java.

Lol! It is blatantly obvious that you are a UOM student. I find it hard to believe that final year students have hardly touched java, even out of pure interest in programming. A search on google heads plenty of results which do exactly what you want as well.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Program to calculate days difference between two dates in java."
Thread Thread Starter Forum Replies Last Post
How to Calculate Expiration Dates in Excel Kungfu Pandey Windows Software 1 07-01-2012 04:22 PM
Auto calculate the amount of stays according to selected dates Macario Software Development 4 17-11-2009 10:13 PM
Calculate duration rather than # of days between two dates SianH Microsoft Project 3 06-10-2009 08:13 AM
Calculating difference in days between two dates in Asp.Net Jagriti Software Development 3 08-07-2009 12:23 AM
How to calculate two dates in Excel Ektaa Windows Software 5 03-03-2009 05:50 PM


All times are GMT +5.5. The time now is 03:55 AM.