Java Date Difference diff(int type, Date date1, Date date2)

Here you can find the source of diff(int type, Date date1, Date date2)

Description

diff

License

Apache License

Parameter

Parameter Description
type a parameter
date1 a parameter
date2 a parameter

Declaration

public static long diff(int type, Date date1, Date date2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**/*w  w  w. ja v a 2s .  c  o m*/
     * 
     * @param type
     * @param date1
     * @param date2
     * @return
     */
    public static long diff(int type, Date date1, Date date2) {
        switch (type) {
        case Calendar.YEAR:
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            long time = calendar.get(Calendar.YEAR);
            calendar.setTime(date2);
            return time - calendar.get(Calendar.YEAR);

        case Calendar.DATE:
            time = date1.getTime() / 1000 / 60 / 60 / 24;
            return time - date2.getTime() / 1000 / 60 / 60 / 24;

        case Calendar.HOUR:
            time = date1.getTime() / 1000 / 60 / 60;
            return time - date2.getTime() / 1000 / 60 / 60;

        case Calendar.SECOND:
            time = date1.getTime() / 1000;
            return time - date2.getTime() / 1000;
        }
        return date1.getTime() - date2.getTime();
    }
}

Related

  1. diff(Date date1, Date date2)
  2. diff(Date date1, Date date2)
  3. diff(Date endDate, Date startDate)
  4. diff(Date sDate, Date fDate)
  5. diff(Date subtrahend, Date minuend, long diffField)
  6. diff_in_date(Date d1, Date d2, String type)
  7. diffBetweenMonth(Date d1, Date d2)
  8. diffCommercial(Date dateUntil, Date dateFrom, boolean bAddDay)
  9. diffDate(Date date, Date date1)