date difference calculation [ 775 views ]
Goal: get the time difference between two dates
The following code gives the difference between two times in this form: 1h 12m
public static String getTimeDifference(Date Start, Date End){ String ret = ""; //in milliseconds long diff = End.getTime() - Start.getTime(); long diffSeconds = diff / 1000 % 60; long diffMinutes = diff / (60 * 1000) % 60; long diffHours = diff / (60 * 60 * 1000) % 24; long diffDays = diff / (24 * 60 * 60 * 1000); if(diffHours >0 ){ ret = diffHours + "h "; } ret += diffMinutes + "m"; return ret; }
The seconds and the days not used. Just good to know how can calculate these values.