# Thread: Program to calculate days difference between two dates in java.

1. 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.  Reply With Quote

2. Member
Join Date
Nov 2005
Posts
1,323

## 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. Member
Join Date
Oct 2005
Posts
2,393

## 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. Member
Join Date
May 2008
Posts
2,389

## 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.");
}
}```  Reply With Quote

5. Member
Join Date
Feb 2008
Posts
1,852

## 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)) {
dBetween++;
}
return dBetween;
}
12. }```  Reply With Quote

6. 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

java, class, date, integer, string, object, days 