Java Calendar Format formatDuration(Calendar t1, Calendar t2, boolean precise)

Here you can find the source of formatDuration(Calendar t1, Calendar t2, boolean precise)

Description

format Duration

License

Open Source License

Declaration

public static String formatDuration(Calendar t1, Calendar t2, boolean precise) 

Method Source Code

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

Related

  1. FormatDate(Calendar p_date, String p_separator)
  2. formatDate(String formats, Calendar date)
  3. formatDateTime(Calendar cal)
  4. formatDateTimeUTC(Calendar datetime)
  5. formatDay(java.util.Calendar d)
  6. formatRfc3339Calendar(Calendar cal)
  7. formatTime(Calendar cal, StringBuffer sb)
  8. formatToHTTPDate(Calendar calendar)
  9. getDate(Calendar cal, String dateFormat)