Here you can find the source of dateDiff(java.util.Date a, java.util.Date b)
Parameter | Description |
---|---|
a | the a |
b | the b |
public static int dateDiff(java.util.Date a, java.util.Date b)
//package com.java2s; //License from project: Apache License import java.util.Calendar; public class Main { /**/*from w w w.j a va 2s. c om*/ * Date diff. * * @param a the a * @param b the b * @return the int */ public static int dateDiff(java.util.Date a, java.util.Date b) { int tempDifference = 0; int difference = 0; Calendar earlier = Calendar.getInstance(); Calendar later = Calendar.getInstance(); if (a.compareTo(b) < 0) { earlier.setTime(a); later.setTime(b); } else { earlier.setTime(b); later.setTime(a); } while (earlier.get(Calendar.YEAR) != later.get(Calendar.YEAR)) { tempDifference = 365 * (later.get(Calendar.YEAR) - earlier.get(Calendar.YEAR)); difference += tempDifference; earlier.add(Calendar.DAY_OF_YEAR, tempDifference); } if (earlier.get(Calendar.DAY_OF_YEAR) != later.get(Calendar.DAY_OF_YEAR)) { tempDifference = later.get(Calendar.DAY_OF_YEAR) - earlier.get(Calendar.DAY_OF_YEAR); difference += tempDifference; earlier.add(Calendar.DAY_OF_YEAR, tempDifference); } return difference; } }