Java TimeUnit Usage formatElapsedTime(long millis)

Here you can find the source of formatElapsedTime(long millis)

Description

format Elapsed Time

License

Open Source License

Declaration

public static String formatElapsedTime(long millis) 

Method Source Code

//package com.java2s;
/*/*from w  ww  .j  a  va  2  s . c o m*/
 * This file is part of the 3D City Database Importer/Exporter.
 * Copyright (c) 2007 - 2013
 * Institute for Geodesy and Geoinformation Science
 * Technische Universitaet Berlin, Germany
 * http://www.gis.tu-berlin.de/
 * 
 * The 3D City Database Importer/Exporter program is free software:
 * you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, see 
 * <http://www.gnu.org/licenses/>.
 * 
 * The development of the 3D City Database Importer/Exporter has 
 * been financially supported by the following cooperation partners:
 * 
 * Business Location Center, Berlin <http://www.businesslocationcenter.de/>
 * virtualcitySYSTEMS GmbH, Berlin <http://www.virtualcitysystems.de/>
 * Berlin Senate of Business, Technology and Women <http://www.berlin.de/sen/wtf/>
 */

import java.util.concurrent.TimeUnit;

public class Main {
    public static String formatElapsedTime(long millis) {
        long d = TimeUnit.MILLISECONDS.toDays(millis);
        long h = TimeUnit.MILLISECONDS.toHours(millis) % TimeUnit.DAYS.toHours(1);
        long m = TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1);
        long s = TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1);

        if (d > 0)
            return String.format("%02d d, %02d h, %02d m, %02d s", d, h, m, s);
        if (h > 0)
            return String.format("%02d h, %02d m, %02d s", h, m, s);
        if (m > 0)
            return String.format("%02d m, %02d s", m, s);

        return String.format("%02d s", s);
    }
}

Related

  1. formatDurationSeconds(long seconds)
  2. formatDurationTill(long start)
  3. formatElapsedSecs(long secs)
  4. formatElapsedTime(final long seconds)
  5. formatElapsedTime(long elapsedTimeMs)
  6. formatInterval(final long l)
  7. formatMillis(final long millis)
  8. formatMillis(final long millis)
  9. formatMillis(long millis)