Here you can find the source of diff(int type, Date date1, Date date2)
Parameter | Description |
---|---|
type | a parameter |
date1 | a parameter |
date2 | a parameter |
public static long diff(int type, Date date1, Date date2)
//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(); } }