difference In Days between two Calendar - Java java.util

Java examples for java.util:Day

Description

difference In Days between two Calendar

Demo Code


//package com.java2s;

import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar date1 = Calendar.getInstance();
        Calendar date2 = Calendar.getInstance();
        System.out.println(differenceInDays(date1, date2));
    }//  www  .ja  v  a2 s.  c  o m

    public static long differenceInDays(Calendar date1, Calendar date2) {
        final long msPerDay = 1000 * 60 * 60 * 24;

        final long date1Milliseconds = date1.getTime().getTime();
        final long date2Milliseconds = date2.getTime().getTime();
        final long result = (date1Milliseconds - date2Milliseconds)
                / msPerDay;

        return result;
    }
}

Related Tutorials