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.");
}
}
Bookmarks