Java Milliseconds calculateHMSFromMilliseconds(long timeInMilliSeconds)

Here you can find the source of calculateHMSFromMilliseconds(long timeInMilliSeconds)

Description

Converts the time in seconds into hours, minutes and seconds.

License

Open Source License

Parameter

Parameter Description
timeInMilliSeconds The time in milliseconds to convert.

Return

See above.

Declaration

public static String calculateHMSFromMilliseconds(long timeInMilliSeconds) 

Method Source Code

//package com.java2s;
/*/*  w w w  . j  a  v a 2s  .c o m*/
 * org.openmicroscopy.shoola.util.ui.UIUtilities
 *
 *------------------------------------------------------------------------------
 *  Copyright (C) 2006-2015 University of Dundee. All rights reserved.
 *
 *
 *  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; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *------------------------------------------------------------------------------
 */

public class Main {
    /**
     * Converts the time in seconds into hours, minutes and seconds.
     * 
     * @param timeInMilliSeconds The time in milliseconds to convert.
     * @return See above.
     */
    public static String calculateHMSFromMilliseconds(long timeInMilliSeconds) {
        return calculateHMS((int) (timeInMilliSeconds / 1000), false);
    }

    /**
     * Converts the time in seconds into hours, minutes and seconds.
     * 
     * @param timeInMilliSeconds The time in milliseconds to convert.
     * @param shortUnit Pass <code>true</code> to use short units
     * e.g. s for second, <code>false</code> otherwise.
     * @return See above.
     */
    public static String calculateHMSFromMilliseconds(long timeInMilliSeconds, boolean shortUnit) {
        return calculateHMS((int) (timeInMilliSeconds / 1000), shortUnit);
    }

    /**
     * Converts the time in seconds into hours, minutes and seconds.
     * 
     * @param timeInSeconds The time in seconds to convert.
     * @param shortUnit Pass <code>true</code> to use short units
     * e.g. s for second, <code>false</code> otherwise.
     * @return See above.
     */
    public static String calculateHMS(int timeInSeconds, boolean shortUnit) {
        int hours = timeInSeconds / 3600;
        timeInSeconds = timeInSeconds - (hours * 3600);
        int minutes = timeInSeconds / 60;
        timeInSeconds = timeInSeconds - (minutes * 60);
        int seconds = timeInSeconds;
        StringBuffer text = new StringBuffer();
        if (hours > 0) {
            text.append(hours);
            if (shortUnit)
                text.append("h");
            else {
                text.append(" hour");
                if (hours > 1)
                    text.append("s");
            }
        }

        if (minutes > 0) {
            text.append(" ");
            text.append(minutes);
            if (shortUnit)
                text.append("min");
            else {
                text.append(" minute");
                if (minutes > 1)
                    text.append("s");
            }
        }
        if (seconds > 0) {
            text.append(" ");
            text.append(seconds);
            if (shortUnit)
                text.append("s");
            else {
                text.append(" second");
                if (seconds > 1)
                    text.append("s");
            }
        }
        return text.toString();
    }

    /**
     * Converts the time in seconds into hours, minutes and seconds.
     * 
     * @param timeInSeconds The time in seconds to convert.
     * @return See above.
     */
    public static String calculateHMS(int timeInSeconds) {
        return calculateHMS(timeInSeconds, false);
    }
}

Related

  1. appendMillis(int millis, StringBuilder builder)
  2. calcDate(long millisecs)
  3. calculateDaysBackIntoMilliseconds(Integer value)
  4. computeEclipseTimeMilliseconds(final long beginTime)
  5. concatMillis(String name)
  6. CreateDate(long milliseconds)
  7. createFromMillis(long milliseconds)