Java Hour Format formatTime(long aTime)

Here you can find the source of formatTime(long aTime)

Description

Format the time in milliseconds into a string in the format dd:hh:mm:ss

License

Apache License

Parameter

Parameter Description
aTime an elapsed time in ms

Return

the formated time string

Declaration

public static String formatTime(long aTime) 

Method Source Code

//package com.java2s;
/*// w  ww.j av a 2  s .c  o m
 *  Copyright 2006 The National Library of New Zealand
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.text.DecimalFormat;

public class Main {
    /**
     * Format the time in milliseconds into a string in the format dd:hh:mm:ss
     * @param aTime an elapsed time in ms
     * @return the formated time string
     */
    public static String formatTime(long aTime) {
        long remainder = 0;
        long days = 0;
        long hours = 0;
        long minutes = 0;
        long seconds = 0;

        days = aTime / 86400000;
        remainder = aTime - (days * 86400000);

        hours = remainder / 3600000;
        remainder = remainder - (hours * 3600000);

        minutes = remainder / 60000;
        remainder = remainder - (minutes * 60000);

        seconds = remainder / 1000;

        DecimalFormat df = new DecimalFormat("00");

        return df.format(days) + ":" + df.format(hours) + ":"
                + df.format(minutes) + ":" + df.format(seconds);
    }
}

Related

  1. formatTime(final Calendar cal)
  2. formatTime(final Date date)
  3. formatTime(final long time)
  4. formatTime(int msec, String format)
  5. formatTime(java.util.Date date)
  6. formatTime(long ms)
  7. formatTime(long time)
  8. formatTime(long time)
  9. formatTime(long time)