Here you can find the source of formatTimeDiff(Calendar ref, Calendar now, boolean ignoreMS)
public static String formatTimeDiff(Calendar ref, Calendar now, boolean ignoreMS)
//package com.java2s; /*/*from w w w. j a va 2 s. c om*/ * Copyright (C) 2011 Sony Ericsson Mobile Communications AB * Copyright (C) 2012-2013 Sony Mobile Communications AB * * This file is part of ChkBugReport. * * ChkBugReport is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * ChkBugReport is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ChkBugReport. If not, see <http://www.gnu.org/licenses/>. */ import java.util.Calendar; public class Main { public static String formatTimeDiff(Calendar ref, Calendar now, boolean ignoreMS) { if (ref != null && now != null) { long diff = now.getTimeInMillis() - ref.getTimeInMillis(); StringBuffer sb = new StringBuffer(); if (diff < 0) { sb.append('-'); diff = -diff; } else { sb.append('+'); } int msec = (int) (diff % 1000); diff /= 1000; if (!ignoreMS) { sb.insert(1, "ms"); sb.insert(1, msec); } if (diff > 0 || ignoreMS) { int sec = (int) (diff % 60); diff /= 60; sb.insert(1, "s"); sb.insert(1, sec); } if (diff > 0) { int min = (int) (diff % 60); diff /= 60; sb.insert(1, "m"); sb.insert(1, min); } if (diff > 0) { int hour = (int) (diff % 24); diff /= 24; sb.insert(1, "h"); sb.insert(1, hour); } if (diff > 0) { sb.insert(1, "d"); sb.insert(1, diff); } return sb.toString(); } return null; } }