Java Calendar Calculate calcTime(Date end, Date start, int calendarField)

Here you can find the source of calcTime(Date end, Date start, int calendarField)

Description

calc Time

License

Open Source License

Declaration

public static long calcTime(Date end, Date start, int calendarField) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {

    public static long calcTime(Date end, Date start, int calendarField) {
        if (end == null || start == null)
            return 0;
        long diff = end.getTime() - start.getTime();

        switch (calendarField) {
        case Calendar.MILLISECOND:
            return diff;

        case Calendar.SECOND:
            return diff / 1000;

        case Calendar.MINUTE:
            return diff / (1000 * 60);

        case Calendar.HOUR:
            return diff / (1000 * 60 * 24);
        default://from  ww w.ja v a2 s .c  om
            throw new RuntimeException("Not support.");
        }
    }
}

Related

  1. alignSecond(Calendar timestamp)
  2. cal2Str(Calendar pCal)
  3. calcAge(Calendar dateOfBirth, Calendar now)
  4. calcDifferenceAsDays(Calendar aBaseDate, Calendar aTargetDate)
  5. calcDifferenceAsYearsPrivate(Calendar aBaseDate, Calendar aTargetDate)
  6. calcTimezone(Calendar cal)
  7. calculateDuration(Calendar para_c1, Calendar para_c2)
  8. calculateHourDiff(Calendar cal1, Calendar cal2)
  9. calculateMinimalDaysInFirstWeek(Calendar calendar)