Java Calendar Different getMillisDiff(long lhs, long rhs, int gregorianCalendarUnits)

Here you can find the source of getMillisDiff(long lhs, long rhs, int gregorianCalendarUnits)

Description

get Millis Diff

License

Open Source License

Declaration

public static float getMillisDiff(long lhs, long rhs, 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.// ww w .  ja  va  2 s . c om
 *
 * 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;

public class Main {
    private static final double SECOND = 1000;
    private static final double MINUTE = SECOND * 60;
    private static final double HOUR = MINUTE * 60;
    private static final double DAY = HOUR * 24;
    private static final double WEEK = DAY * 7;
    private static final double YEAR = DAY * 365.25;

    public static float getMillisDiff(long lhs, long rhs, int gregorianCalendarUnits) {
        long millis = lhs - rhs;
        switch (gregorianCalendarUnits) {
        case Calendar.MILLISECOND:
            return millis;
        case Calendar.SECOND:
            return (float) (millis / SECOND);
        case Calendar.MINUTE:
            return (float) (millis / MINUTE);
        case Calendar.HOUR:
            return (float) (millis / HOUR);
        case Calendar.DAY_OF_WEEK:
        case Calendar.DAY_OF_MONTH:
        case Calendar.DAY_OF_YEAR:
            return (float) (millis / DAY);
        case Calendar.WEEK_OF_MONTH:
        case Calendar.WEEK_OF_YEAR:
            return (float) (millis / WEEK);
        case Calendar.YEAR:
            return (float) (millis / YEAR);
        default:
            return 0.0f;
        }
    }
}

Related

  1. getDifference(int field, Calendar aCalX, Calendar aCalY)
  2. getDifferenceInDays(Calendar fromDate, Calendar toDate)
  3. getDifferenceInSeconds(Calendar calendarStart, Calendar calendarEnd)
  4. getDiffPartialYears(Date date, GregorianCalendar b)
  5. getDiffTargetDate(Calendar cal, int diffDay)
  6. getMonthDiff(Calendar c1, Calendar c2)
  7. getMonthsDifference(Calendar date1, Calendar date2)
  8. getTargetDate(Calendar reqCal, int diffMon, int proxyDay)
  9. getTimeDifference(String unit, Calendar start, Calendar end)