Java Calendar Different formatTimeDiff(Calendar ref, Calendar now, boolean ignoreMS)

Here you can find the source of formatTimeDiff(Calendar ref, Calendar now, boolean ignoreMS)

Description

format Time Diff

License

Open Source License

Declaration

public static String formatTimeDiff(Calendar ref, Calendar now, boolean ignoreMS) 

Method Source Code

//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;
    }
}

Related

  1. dateDifferenceInMin(Calendar startDate, Calendar stopDate)
  2. diff(Calendar value1, Calendar value2, int millisInCondition)
  3. diffDay(Calendar calendar, int day)
  4. diffDays(Calendar startDate, Calendar endDate)
  5. differenceInDays(Calendar date1, Calendar date2)
  6. getDateDiff(Calendar c1, Calendar c2)
  7. getDateDiff(Calendar d1, Calendar d2)
  8. getDateDiff(Date d1, Date d2, int gregorianCalendarUnits)
  9. getDaysDifference(Calendar refDate, Date date)