Java Time Readable Format toTimeString(long millis)

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

Description

to Time String

License

Open Source License

Declaration

public static String toTimeString(long millis) 

Method Source Code

//package com.java2s;
/*//from   w ww .  java 2  s .c o m
 * #%L
 * Intuit Tank Api
 * %%
 * Copyright (C) 2011 - 2015 Intuit Inc.
 * %%
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * #L%
 */

public class Main {
    public static String toTimeString(long millis) {
        int ms = (int) (millis % 1000);
        int sec = (int) (Math.floor(millis / 1000)) % 60;
        int min = (int) (Math.floor(millis / (1000 * 60))) % 60;
        int hours = (int) (Math.floor(millis / (1000 * 60 * 60))) % 24;
        int days = (int) (Math.floor(millis / (1000 * 60 * 60 * 24)));
        StringBuilder sb = new StringBuilder();
        if (days > 0) {
            sb.append(days).append("d");
        }
        if (hours > 0) {
            if (sb.length() > 0) {
                sb.append(' ');
            }
            sb.append(hours).append("h");
        }
        if (min > 0) {
            if (sb.length() > 0) {
                sb.append(' ');
            }
            sb.append(min).append("m");
        }
        if (sec > 0) {
            if (sb.length() > 0) {
                sb.append(' ');
            }
            sb.append(sec).append("s");
        }
        if (ms > 0) {
            if (sb.length() > 0) {
                sb.append(' ');
            }
            sb.append(ms).append("ms");
        }
        if (sb.length() == 0) {
            sb.append('0');
        }
        return sb.toString();
    }
}

Related

  1. toTimeSpanDescription(long time)
  2. toTimestamp(long time)
  3. toTimeStampString(String timeStr)
  4. toTimeString(double millis)
  5. toTimeString(long frame)
  6. toTimeString(long millis, boolean showSeconds, String separatorHours, String separatorMin, String separatorSec)
  7. toTimeString(long seconds)
  8. toTimeStringForceShowHours(long millis, boolean showSeconds, boolean showUnits)