Java Millisecond Format format(final long timeInMillis)

Here you can find the source of format(final long timeInMillis)

Description

Formats milliseconds into a String date representation of:
solely seconds when less than 59999 or minutes and seconds when more.

License

Open Source License

Parameter

Parameter Description
timeInMillis a parameter

Return

date representation

Declaration

public static final String format(final long timeInMillis) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static final SimpleDateFormat SIMPLE_DATE_MINUTE_SECONDS_FORMAT = new SimpleDateFormat("mm:ss");
    private static final SimpleDateFormat SIMPLE_DATE_SECONDS_FORMAT = new SimpleDateFormat("ss");
    private static final long NUMBER_OF_MILLISECONDS_LESS_THAN_MINUTE = 59999;

    /**//from  www.jav  a 2s  .co  m
     * Formats milliseconds into a {@link String} date representation of:</br>
     * solely seconds when less than 59999 or minutes and seconds when more.
     * @param timeInMillis
     * @return date representation
     */
    public static final String format(final long timeInMillis) {
        return (isLessThanMinute(timeInMillis) ? SIMPLE_DATE_SECONDS_FORMAT : SIMPLE_DATE_MINUTE_SECONDS_FORMAT)
                .format(new Date(timeInMillis));
    }

    /**
     * Checks if a number of milliseconds is less than a minute or not.
     * @param timeInMillis
     * @return <code>true</code> when less than a minute, otherwise <code>false</code>
     */
    public static final boolean isLessThanMinute(final long timeInMillis) {
        return timeInMillis >= 0 && timeInMillis < NUMBER_OF_MILLISECONDS_LESS_THAN_MINUTE;
    }
}

Related

  1. createDateTimeString(final long ms, final DateFormat timeFormat, final String milliSep)
  2. format(long millis)
  3. format(long millis)
  4. format(long millis)
  5. format(long millis)