본문 바로가기

Java/__Base

Java - How to find date difference


import java.util.Calendar;

public class DateDiff {

    public static void main(String[] args) {

        Calendar ca1 = Calendar.getInstance();
        Calendar ca2 = Calendar.getInstance();

        // Set the date for both of the calendar to get difference
        ca1.set(2008, 07, 27);
        ca2.set(2009, 05, 15);

        // Get date in milliseconds
        long milisecond1 = ca1.getTimeInMillis();
        long milisecond2 = ca2.getTimeInMillis();

        // Find date difference in milliseconds
        long diffInMSec = milisecond2 - milisecond1;

        // Find date difference in days
        // (24 hours 60 minutes 60 seconds 1000 millisecond)
        long diffOfDays = diffInMSec / (24 * 60 * 60 * 1000);

        System.out.println("Date Difference in : " + diffOfDays + " days.");

    }
}