Here you can find the source of DateDiff(String strDateBegin, String strDateEnd, int iType)
public static int DateDiff(String strDateBegin, String strDateEnd, int iType)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; import java.util.GregorianCalendar; public class Main { public static int DateDiff(String strDateBegin, String strDateEnd, int iType) { Calendar calBegin = parseDateTime(strDateBegin); Calendar calEnd = parseDateTime(strDateEnd); long lBegin = calBegin.getTimeInMillis(); long lEnd = calEnd.getTimeInMillis(); int ss = (int) ((lBegin - lEnd) / 1000L); int min = ss / 60; int hour = min / 60; int day = hour / 24; if (iType == 0) return hour; if (iType == 1) return min; if (iType == 2) return day; else//from w w w . ja va2 s .c o m return -1; } public static Calendar parseDateTime(String baseDate) { Calendar cal = null; cal = new GregorianCalendar(); int yy = Integer.parseInt(baseDate.substring(0, 4)); int mm = Integer.parseInt(baseDate.substring(5, 7)) - 1; int dd = Integer.parseInt(baseDate.substring(8, 10)); int hh = 0; int mi = 0; int ss = 0; if (baseDate.length() > 12) { hh = Integer.parseInt(baseDate.substring(11, 13)); mi = Integer.parseInt(baseDate.substring(14, 16)); ss = Integer.parseInt(baseDate.substring(17, 19)); } cal.set(yy, mm, dd, hh, mi, ss); return cal; } }