Java Time Format formatTimePeriod(long timePeriod)

Here you can find the source of formatTimePeriod(long timePeriod)

Description

Formats a time period in legible terms - not a date represented as a long, but a period of time (eg processing time)

License

Open Source License

Parameter

Parameter Description
timePeriod time period to format

Declaration

public static final String formatTimePeriod(long timePeriod) 

Method Source Code

//package com.java2s;
/*// w w w  . ja  v  a  2s  .c  o m
Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
This file is part of the Semantic Discovery Toolkit.
    
The Semantic Discovery Toolkit 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.
    
The Semantic Discovery Toolkit 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 The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
*/

public class Main {
    /**
     * Formats a time period in legible terms - not a date represented as a long,
     * but a period of time (eg processing time)
     * @param timePeriod time period to format
     */
    public static final String formatTimePeriod(long timePeriod) {
        int hours = (int) timePeriod / 3600000;
        timePeriod = timePeriod % 3600000;
        int minutes = (int) timePeriod / 60000;
        timePeriod = timePeriod % 60000;
        int seconds = (int) timePeriod / 1000;
        timePeriod = timePeriod % 1000;

        return (hours > 0 ? hours + "h " : "") + (minutes > 0 ? minutes + "m " : "")
                + (seconds > 0 ? seconds + "s " : "") + (timePeriod > 0 ? timePeriod + "ms " : "");
    }
}

Related

  1. formatTimeLikeTimer(long time, boolean appendMs)
  2. formatTimeNicely(long millis)
  3. formatTimeOffset(long offset)
  4. formatTimePart2(long number, String unit)
  5. formatTimePeriod(long millis)
  6. formatTimePeriod(long timestamp)
  7. formatTimeSec(long time)
  8. formatTimespan(int timespan)
  9. formatTimeSpanForScheduler(long time)