Here you can find the source of getTimeDiffMin(String format, String startDate, String endDate)
public static int getTimeDiffMin(String format, String startDate, String endDate)
//package com.java2s; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static int getTimeDiffMin(String format, String startDate, String endDate) { Date sDate = null;//from w w w . j a v a 2s.c o m Date eDate = null; int ret = 0; int sign = 1; try { sDate = (new SimpleDateFormat(format)).parse(startDate); Calendar cal1 = Calendar.getInstance(); cal1.setTime(sDate); eDate = (new SimpleDateFormat(format)).parse(endDate); Calendar cal2 = Calendar.getInstance(); cal2.setTime(eDate); Date date1 = cal1.getTime(); long t1 = date1.getTime(); Date date2 = cal2.getTime(); long t2 = date2.getTime(); long elapsed = t1 - t2; if (elapsed < 0L) { elapsed = -elapsed; sign = -1; } int tmp = (int) (elapsed / 1000L); //int millisec = (int) (elapsed % 1000L); int sec = tmp % 60; tmp /= 60; int mn = tmp % 60; tmp /= 60; int hr = tmp % 24; int dd = tmp / 24; if (dd != 0) ret += (dd * 1440); if (hr != 0) ret += (hr * 60); if (mn != 0) ret += mn; //if (sec != 0) // ret += sec; if (sign < 0) ret = -ret; } catch (ParseException e) { e.printStackTrace(); } return ret; } }