Java Duration Format formatDuration(long durationSec)

Here you can find the source of formatDuration(long durationSec)

Description

format Duration

License

Open Source License

Parameter

Parameter Description
durationSec Duration in seconds

Return

Duration nicely formatted (eg: 37mn, 1h38)

Declaration

public static String formatDuration(long durationSec) 

Method Source Code

//package com.java2s;
/* -------------------------------------------------------------------------
OpenTripPlanner GWT Client/*ww  w . j a v  a  2s  .c o  m*/
Copyright (C) 2015 Mecatran - info@mecatran.com
    
This program 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.
    
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   ------------------------------------------------------------------------- */

public class Main {
    /**
     * @param durationSec
     *            Duration in seconds
     * @return Duration nicely formatted (eg: 37mn, 1h38)
     */
    public static String formatDuration(long durationSec) {
        long min = (durationSec + 30L) / 60L;
        long hour = min / 60L;
        min %= 60L;
        if (hour == 0L) {
            return min + smallerFont("mn");
        } else {
            return hour + smallerFont("h") + (min < 10 ? "0" : "") + min;
        }
    }

    /**
     * Make a font smaller (css font class 'smaller').
     * 
     * @param html
     * @return
     */
    public static String smallerFont(String html) {
        return "<span class='smaller'>" + html + "</span>";
    }
}

Related

  1. formatDuration(long duration)
  2. formatDuration(long duration, boolean displayMilliseconds)
  3. formatDuration(long duration, boolean simplify, boolean includeMillies)
  4. formatDuration(long durationInMs)
  5. formatDuration(long durationMillis)
  6. formatDuration(long elapsed)
  7. formatDuration(long elapsedSec)
  8. formatDuration(Long input)
  9. formatDuration(long milis)