Java Millisecond Format formatTimeInMillis(long millis)

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

Description

Formats a delta time.

License

Open Source License

Parameter

Parameter Description
millis delta timestamp in millis

Return

formatted string

Declaration

public static String formatTimeInMillis(long millis) 

Method Source Code

//package com.java2s;
/*/*from w  w  w  . ja v a  2 s.co m*/
 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Sly Technologies, Inc.
 *
 * This file is part of jNetPcap.
 *
 * jNetPcap 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/>.
 */

public class Main {
    /** The Constant DAY_MILLIS. */
    private final static int DAY_MILLIS = 24 * 60 * 60 * 1000;
    /** The Constant HOUR_MILLIS. */
    private final static int HOUR_MILLIS = 60 * 60 * 1000;
    /** The Constant MINUTE_MILLIS. */
    private final static int MINUTE_MILLIS = 60 * 1000;
    /** The Constant SECOND_MILLIS. */
    private final static int SECOND_MILLIS = 1000;
    /** The Constant WEEK_MILLIS. */
    private final static int WEEK_MILLIS = 7 * 24 * 60 * 60 * 1000;

    /**
     * Formats a delta time.
     * 
     * @param millis
     *            delta timestamp in millis
     * @return formatted string
     */
    public static String formatTimeInMillis(long millis) {

        StringBuilder b = new StringBuilder();

        long u = 0;

        while (millis > 0) {
            if (b.length() != 0) {
                b.append(' ');
            }

            if (millis > WEEK_MILLIS) {
                u = millis / WEEK_MILLIS;
                b.append(u).append(' ').append((u > 1) ? "weeks" : "week");
                millis -= u * WEEK_MILLIS;

            } else if (millis > DAY_MILLIS) {
                u = millis / DAY_MILLIS;
                b.append(u).append(' ').append((u > 1) ? "days" : "day");
                millis -= u * DAY_MILLIS;

            } else if (millis > HOUR_MILLIS) {
                u = millis / HOUR_MILLIS;
                b.append(u).append(' ').append((u > 1) ? "days" : "day");
                millis -= u * HOUR_MILLIS;

            } else if (millis > MINUTE_MILLIS) {
                u = millis / MINUTE_MILLIS;
                b.append(u).append(' ').append((u > 1) ? "minutes" : "minute");
                millis -= u * MINUTE_MILLIS;

            } else if (millis > SECOND_MILLIS) {
                u = millis / SECOND_MILLIS;
                b.append(u).append(' ').append((u > 1) ? "seconds" : "second");
                millis -= u * SECOND_MILLIS;

            } else if (millis > 0) {
                u = millis;
                b.append(u).append(' ').append((u > 1) ? "millis" : "milli");
                millis -= u;
            }
        }

        return b.toString();
    }
}

Related

  1. formattedMillis(long millis)
  2. formatTime(long currentTimeMillis)
  3. formatTime(long localCreationTimeMillis)
  4. formatTime(long millis)
  5. formatTime(long millis)
  6. formatTimeMillis(long millis)
  7. formatTimeMillis(long timeMillis)
  8. formatTimestamp(long millis)
  9. formatTimeWithMillis(double time)