09 April 2012

Calculate the difference in days between two dates.



package one;
import java.util.*;

public class Difference {
public static void main(String[] args)
{
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();

cal1.set(2006, 12, 30);
cal2.set(2007, 5, 3);

long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();

long diff = milis2 - milis1;

long diffDays = diff / (24 * 60 * 60 * 1000);

System.out.println("Date difference in days: " + diffDays + " days.");
}
}

0 comments:

Post a Comment

Related Posts with Thumbnails

Popular Posts

 
;