Java Duration Format formatDuration(Duration duration, boolean inProgress)

Here you can find the source of formatDuration(Duration duration, boolean inProgress)

Description

format Duration

License

Open Source License

Declaration

public static String formatDuration(Duration duration, boolean inProgress) 

Method Source Code


//package com.java2s;
/*/*from w  ww .  ja  v  a  2  s .c  o  m*/
 * Copyright 2015-2016 Todd Kulesza <todd@dropline.net>.
 *
 * This file is part of Archivo.
 *
 * Archivo 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 3 of the License, or
 * (at your option) any later version.
 *
 * Archivo 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 Archivo.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.time.Duration;

public class Main {
    public static String formatDuration(Duration duration, boolean inProgress) {
        int hours = (int) duration.toHours();
        int minutes = (int) duration.toMinutes() - (hours * 60);
        int seconds = (int) (duration.getSeconds() % 60);

        // Round so that we're only displaying hours and minutes
        if (seconds >= 30) {
            minutes++;
        }
        if (minutes >= 60) {
            hours++;
            minutes = 0;
        }

        StringBuilder sb = new StringBuilder();
        if (hours > 0) {
            sb.append(String.format("%d:%02d hour", hours, minutes));
            if (hours > 1 || minutes > 0)
                sb.append("s");
        } else {
            sb.append(String.format("%d minute", minutes));
            if (minutes != 1) {
                sb.append("s");
            }
        }
        if (inProgress)
            sb.append(" (still recording)");

        return sb.toString();
    }
}

Related

  1. formatDuration(Duration duration)
  2. formatDuration(Duration duration)
  3. formatDuration(Duration duration)
  4. formatDuration(Duration duration)
  5. formatDuration(Duration duration)
  6. formatDuration(final Duration duration)
  7. formatDuration(final Duration duration, final boolean wrapInDurationFunction)
  8. formatDuration(final int duration)
  9. formatDuration(final long milliSeconds)