Java Millisecond Format formatElapsedTime(long millis)

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

Description

Format an elapsed time into a plurialization correct string.

License

Open Source License

Parameter

Parameter Description
time The elapsed time to report in milliseconds.

Return

The formatted text in minutes/seconds.

Declaration

public static String formatElapsedTime(long millis) 

Method Source Code


//package com.java2s;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.TimeZone;

public class Main {
    private static final MessageFormat MINUTE_SECONDS = new MessageFormat("{0}{1}");

    /**//from  w  w w. ja v a 2 s.  c  o  m
     *  Format an elapsed time into a plurialization correct string.
     *  It is limited only to report elapsed time in minutes and
     *  seconds and has the following behavior.
     *  <ul>
     *  <li>minutes are not displayed when 0. (ie: "45 seconds")</li>
     *  <li>seconds are always displayed in plural form (ie "0 seconds" or
     *  "10 seconds") except for 1 (ie "1 second")</li>
     *  </ul>
     *
     *  @param  time  The elapsed time to report in milliseconds.
     *  @return The formatted text in minutes/seconds.
     */
    public static String formatElapsedTime(long millis) {
        long seconds = millis / 1000;
        long minutes = seconds / 60;
        Object[] args = { new Long(minutes), new Long(seconds % 60) };
        return MINUTE_SECONDS.format(args);
    }

    /**
     *  Format a date/time into a specific pattern.
     *
     *  @param  date    The date to format expressed in milliseconds.
     *  @param  pattern The pattern to use to format the date.
     *  @return The formatted date.
     */
    public static String format(long date, String pattern) {
        return format(new Date(date), pattern);
    }

    /**
     *  Format a date/time into a specific pattern.
     *
     *  @param  date    The date to format expressed in milliseconds.
     *  @param  pattern The pattern to use to format the date.
     *  @return The formatted date.
     */
    public static String format(Date date, String pattern) {
        DateFormat df = createDateFormat(pattern);
        return df.format(date);
    }

    /**
     *  Return a date format set to GMT time zone.
     *
     *  @param  pattern  Tthe pattern used for date/time formatting.
     *  @return The configured format for this pattern.
     */
    private static DateFormat createDateFormat(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        TimeZone gmt = TimeZone.getTimeZone("GMT");
        sdf.setTimeZone(gmt);
        sdf.setLenient(true);
        return sdf;
    }
}

Related

  1. format(long timeMillis, String pattern)
  2. format_duration(long millis)
  3. formatCcsdsUtcMillisFormat(Date time)
  4. formatDataTimeStamp(final long timeInMillis)
  5. formatElapsedMillis(long time)
  6. formateTime(long milliseconds)
  7. formatMillis(int millis, int digits)
  8. formatMillis(long duration)
  9. formatMillis(long millis)