Java Second Format formatTime(float seconds)

Here you can find the source of formatTime(float seconds)

Description

Formats a time in seconds into HH:MM:SS:MS

License

Open Source License

Parameter

Parameter Description
seconds a parameter

Declaration

public static String formatTime(float seconds) 

Method Source Code


//package com.java2s;
/*//w w w  .  j  av  a  2s. c o m
 * blue - object composition environment for csound
 * Copyright (c) 2000-2005 Steven Yi (stevenyi@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by  the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307 USA
 */

import java.text.MessageFormat;
import java.util.Locale;

public class Main {
    private static final MessageFormat TIME_FORMAT = new MessageFormat("{0}:{1}:{2}:{3}", Locale.ENGLISH);
    private static final Object[] TIME_ARRAY = new Object[4];

    /**
     * Formats a time in seconds into HH:MM:SS:MS
     * 
     * @param seconds
     * @return
     */
    public static String formatTime(float seconds) {
        int s = (int) seconds;

        int ms = (int) ((seconds - s) * 100);

        int h = s / 3600;
        int m = (s - (h * 3600)) / 60;

        s = s % 60;

        String hStr = h < 10 ? "0" + h : Integer.toString(h);
        String mStr = m < 10 ? "0" + m : Integer.toString(m);
        String sStr = s < 10 ? "0" + s : Integer.toString(s);
        String msStr = ms < 10 ? "0" + ms : Integer.toString(ms);

        TIME_ARRAY[0] = hStr;
        TIME_ARRAY[1] = mStr;
        TIME_ARRAY[2] = sStr;
        TIME_ARRAY[3] = msStr;

        return TIME_FORMAT.format(TIME_ARRAY);
    }
}

Related

  1. formatSeconds(long time, String timerPrecisionId, boolean round)
  2. formatSecondsRegular(int seconds)
  3. formatSecondsWithHour(int seconds)
  4. formatTime(Date d, Locale locale, boolean withSeconds)
  5. formatTime(double seconds)
  6. formatTimeInSeconds(int v)
  7. formatTimeSecondsSinceEpoch(final long time, StringBuilder sbuffer)
  8. formatTimeSpanSeconds(long span)
  9. formatTimeWithOutSeconds(java.util.Date d)