Here you can find the source of formatTimeInMillis(long millis)
Parameter | Description |
---|---|
millis | delta timestamp in millis |
public static String formatTimeInMillis(long millis)
//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(); } }