Java Calendar Different getDateDiff(Date d1, Date d2, int gregorianCalendarUnits)

Here you can find the source of getDateDiff(Date d1, Date d2, int gregorianCalendarUnits)

Description

get Date Diff

License

Open Source License

Declaration

public static float getDateDiff(Date d1, Date d2, int gregorianCalendarUnits) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2004-2008 Whirlwind Match Limited. All rights reserved.
 *
 * This is open source software; you can use, redistribute and/or modify
 * it under the terms of the Open Software Licence v 3.0 as published by the 
 * Open Source Initiative./*from   www .j  a  va  2 s  .co  m*/
 *
 * You should have received a copy of the Open Software Licence along with this
 * application. if not, contact the Open Source Initiative (www.opensource.org)
 *****************************************************************************/

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    public static float getDateDiff(Date d1, Date d2, int gregorianCalendarUnits) {
        long millis = d1.getTime() - d2.getTime();
        switch (gregorianCalendarUnits) {
        case Calendar.MILLISECOND:
            return millis;
        case Calendar.SECOND:
            return millis / 1000;
        case Calendar.MINUTE:
            return millis / 1000 / 60;
        case Calendar.HOUR:
            return millis / 1000 / 60 / 60;
        case Calendar.DAY_OF_WEEK:
        case Calendar.DAY_OF_MONTH:
        case Calendar.DAY_OF_YEAR:
            return millis / 1000 / 60 / 60 / 24;
        case Calendar.WEEK_OF_MONTH:
        case Calendar.WEEK_OF_YEAR:
            return millis / 1000 / 60 / 60 / 24 / 7;
        case Calendar.YEAR:
            return (float) (millis / 1000 / 60 / 60 / 24 / 365.25);
        default:
            return 0.0f;
        }
    }

    public static float getDateDiff(GregorianCalendar d1, GregorianCalendar d2, int gregorianCalendarUnits) {
        return getDateDiff(d1.getTime(), d2.getTime(), gregorianCalendarUnits);
    }
}

Related

  1. diffDays(Calendar startDate, Calendar endDate)
  2. differenceInDays(Calendar date1, Calendar date2)
  3. formatTimeDiff(Calendar ref, Calendar now, boolean ignoreMS)
  4. getDateDiff(Calendar c1, Calendar c2)
  5. getDateDiff(Calendar d1, Calendar d2)
  6. getDaysDifference(Calendar refDate, Date date)
  7. getDifference(int constant, Calendar first, Calendar second)
  8. getDifference(int field, Calendar aCalX, Calendar aCalY)
  9. getDifferenceInDays(Calendar fromDate, Calendar toDate)