Here you can find the source of getDiffTime(Date beforeTime, Date afterTime)
public static long getDiffTime(Date beforeTime, Date afterTime)
//package com.java2s; //License from project: Open Source License import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { /**//from ww w . ja va2s .c o m * format pattern : HH:mm:ss */ public static final SimpleDateFormat FORMAT_HMS = new SimpleDateFormat("HH:mm:ss"); public static long getDiffTime(Date beforeTime, Date afterTime) { try { String beforeTimeStr = FORMAT_HMS.format(beforeTime); String afterTimeStr = FORMAT_HMS.format(afterTime); Date bTime = parseTime(beforeTimeStr); Date aTime = parseTime(afterTimeStr); long diff = aTime.getTime() - bTime.getTime(); return diff; } catch (Exception e) { e.printStackTrace(); } return 0; } static public String format(Date date, String formatText) throws Exception { SimpleDateFormat format = new SimpleDateFormat(formatText); return format.format(date); } /** * Parses text in 'HH:mm:ss' format to produce a time. * * @param s * the text * @return Date * @throws ParseException */ static public Date parseTime(String s) throws ParseException { return FORMAT_HMS.parse(s); } }