Java Millisecond Convert millisToStringDouble(double millis)

Here you can find the source of millisToStringDouble(double millis)

Description

millis To String Double

License

Apache License

Parameter

Parameter Description
millis The milliseconds to convert to a string

Return

The exact String representation of the milliseconds. Example: 5415040.999 > 01:30:15.040999

Declaration

public static String millisToStringDouble(double millis) 

Method Source Code

//package com.java2s;
/**//from ww w .j  a v a  2s  . c o m
 * Copyright 2016 Charles-Eugene Loubao
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 {
    private static final DecimalFormat decimalFormat = new DecimalFormat(
            "##.###");

    /**
     * @param millis The milliseconds to convert to a string
     * @return The exact String representation of the milliseconds. Example: 5415040.999 > 01:30:15.040999
     */
    public static String millisToStringDouble(double millis) {

        double seconds = millis / 1000d;

        long hours = (long) (seconds / 3600);
        long minutes = (long) ((seconds % 3600) / 60);
        double remaining = (seconds % 3600d) % 60d;

        String output = "";

        output = output.concat(String.format("%02d:", hours));
        output = output.concat(String.format("%02d:", minutes));

        String stringifiedSeconds = decimalFormat.format(remaining);

        // The decimal part of the seconds (Milliseconds) must be given in three digits.
        // We fill the remaining spaces (if any) with zeros

        String decimal = stringifiedSeconds.split("[.]")[1];
        int toFill = 3 - decimal.length();

        for (int i = 0; i < toFill; i++) {
            decimal = decimal.concat("0");
        }

        stringifiedSeconds = stringifiedSeconds.replace(
                stringifiedSeconds.split("[.]")[1], decimal);

        output = output.concat(stringifiedSeconds);

        return output;
    }
}

Related

  1. millisToShortDHMS(long duration)
  2. millisToString(float millis)
  3. millisToString(long millis)
  4. millisToString(long ms)
  5. millisToString(long t)
  6. millisToText(long millis)
  7. millisToTime(final long time)
  8. millisToTime(float millis)
  9. millisToTime(long ms)