Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date diff function in java

 
saahil sinha
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I like to know if there is any function in java that will perform the diffrence between 2 dates and return it in the no. of days between the 2 dates.If yes what is the function and its syntax
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean something like this:
 
saahil sinha
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Ur code did give me some idea so i did some thinkin and this is what i actually coded and wanted.
import java.util.*;
public class test_time
{ public static final long DAY = 24 * 3600 * 1000;
public static void main(String args[])
{
GregorianCalendar gcl1=new GregorianCalendar(2002,2,29);//entry date
GregorianCalendar gcl2=new GregorianCalendar();//return date
daysBetweenDates(gcl1,gcl2);
}
public static void daysBetweenDates(GregorianCalendar d1,GregorianCalendar d2)
{
Date day1=d1.getTime();
long dd1=day1.getTime();
Date day2=d2.getTime();
long dd2= day2.getTime();
int de=(int)((dd1-dd2)/DAY);
System.out.println(day1+":"+day2+"::"+de);
}
}
Anyways thanx a lot
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic