Here you can find the source of millisToTimeSpan(final long interv)
Parameter | Description |
---|---|
interv | the time interval, in milliseconds |
public static String millisToTimeSpan(final long interv)
//package com.java2s; //License from project: Open Source License import java.text.DecimalFormat; public class Main { /**//ww w . j a v a 2s.c om * Number of milliseconds per minute. */ private static final long MSECS_PER_MIN = 60000L; /** * Number of milliseconds per hour. */ private static final long MSECS_PER_HOUR = MSECS_PER_MIN * 60L; /** * Number of milliseconds per day. */ private static final long MSECS_PER_DAY = MSECS_PER_HOUR * 24L; /** * Number of milliseconds per week. */ private static final long MSECS_PER_WEEK = MSECS_PER_DAY * 7L; /** * Number of milliseconds per year. */ private static final long MSECS_PER_YEAR = 31556926000L; /** * Convert the input argument into a string describing the time span. * * @param interv the time interval, in milliseconds * @return a String describing the time span */ public static String millisToTimeSpan(final long interv) { // Declare our string buffer StringBuffer buf = new StringBuffer(100); // Check for zero and negative values long lMillis = interv; if (lMillis <= 0L) { // The value is either illegal, or zero, so return buf.append("0 seconds"); return buf.toString(); } // Get the number of years final long lYears = (long) (lMillis / MSECS_PER_YEAR); lMillis = lMillis % MSECS_PER_YEAR; // Get the number of weeks final long lWeeks = (long) (lMillis / MSECS_PER_WEEK); // Update lMillis with the remainder lMillis = lMillis % MSECS_PER_WEEK; // Get the number of days final long lDays = (long) (lMillis / MSECS_PER_DAY); // Update lMillis with the remainder lMillis = lMillis % MSECS_PER_DAY; // Get the number of hours final long lHours = (long) (lMillis / MSECS_PER_HOUR); // Update lMillis with the remainder lMillis = lMillis % MSECS_PER_HOUR; // Get the number of minutes final long lMinutes = (long) (lMillis / MSECS_PER_MIN); // Update lMillis with the remainder lMillis = lMillis % MSECS_PER_MIN; // Get the number of seconds final float fSeconds = (float) (((float) lMillis) / 1000.0F); if (lYears > 0L) { // Add the number and unit buf.append(Long.toString(lYears)).append(" year"); // Make the unit plural, if necessary if (lYears > 1L) { buf.append('s'); } } // Now generate the string. First check if there are any weeks. if (lWeeks > 0L) { // Append a leading comma, if necessary, and then add the number and unit if (buf.length() > 0) { buf.append(", "); } // Add the number and unit buf.append(Long.toString(lWeeks)).append(" week"); // Make the unit plural, if necessary if (lWeeks > 1L) { buf.append('s'); } } // Check if there are any days. if (lDays > 0L) { // Append a leading comma, if necessary, and then add the number and unit if (buf.length() > 0) { buf.append(", "); } buf.append(Long.toString(lDays)).append(" day"); // Make the unit plural, if necessary if (lDays > 1L) { buf.append('s'); } } // Check if there are any hours. if (lHours > 0L) { // Append a leading comma, if necessary, and then add the number and unit if (buf.length() > 0) { buf.append(", "); } buf.append(Long.toString(lHours)).append(" hour"); // Make the unit plural, if necessary if (lHours > 1L) { buf.append('s'); } } // Check if there are any minutes. if (lMinutes > 0L) { // Append a leading comma, if necessary, and then add the number and unit if (buf.length() > 0) { buf.append(", "); } buf.append(Long.toString(lMinutes)).append(" minute"); // Make the unit plural, if necessary if (lMinutes > 1L) { buf.append('s'); } } // Check if there are any seconds. if (Float.compare(fSeconds, 0.0F) > 0) { // Append a leading comma, if necessary if (buf.length() > 0) { buf.append(", "); } // Format it because it's a floating point number DecimalFormat df = new DecimalFormat(); df.setDecimalSeparatorAlwaysShown(false); df.setMaximumFractionDigits(3); buf.append(df.format((double) fSeconds)).append(" second"); // Make the unit plural, if necessary (if the number is anything but 1.0) if (Float.compare(fSeconds, 1.0F) != 0) { buf.append('s'); } } // Return the string return buf.toString(); } }