Here you can find the source of formatDuration(Calendar t1, Calendar t2, boolean precise)
public static String formatDuration(Calendar t1, Calendar t2, boolean precise)
//package com.java2s; //License from project: Open Source License import java.util.Calendar; public class Main { public static String formatDuration(long d, boolean precise) { if (precise) { long seconds = d % 60; d = d / 60;//from ww w. ja va2 s.c o m long minutes = d % 60; long hours = d / 60; // The string return String.format("%02d:%02d:%02d", hours, minutes, seconds); } else { long minutes = d / 60; if (minutes > 0) return minutes + " minute" + (minutes != 1 ? "s" : ""); else return d + " second" + (d != 1 ? "s" : ""); } } public static String formatDuration(Calendar t1, Calendar t2, boolean precise) { // Convert to duration in seconds return formatDuration(getDuration(t1, t2), precise); } public static long getDuration(Calendar t1, Calendar t2) { return (t2.getTimeInMillis() - t1.getTimeInMillis()) / 1000; } }