Here you can find the source of dateDiff(int type, Calendar fromDate, Calendar toDate)
Parameter | Description |
---|---|
type | The calendar field type, see Calendar documentation. |
fromDate | The calendar to check from. |
toDate | The calendar to check against. |
static int dateDiff(int type, Calendar fromDate, Calendar toDate)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; public class Main { /**//from w ww . j a va 2 s. com * Get the difference of a specific calendar field for 2 calendar instances. * * @param type The calendar field type, see {@link Calendar} documentation. * @param fromDate The calendar to check from. * @param toDate The calendar to check against. * @return The field difference. */ static int dateDiff(int type, Calendar fromDate, Calendar toDate) { boolean future = toDate.after(fromDate); int diff = 0; long savedDate = fromDate.getTimeInMillis(); while ((future && !fromDate.after(toDate)) || (!future && !fromDate.before(toDate))) { savedDate = fromDate.getTimeInMillis(); fromDate.add(type, future ? 1 : -1); diff++; } diff--; fromDate.setTimeInMillis(savedDate); return diff; } }