Java Milli Second to Minute asHoursMinutesSeconds(long milliseconds)

Here you can find the source of asHoursMinutesSeconds(long milliseconds)

Description

as Hours Minutes Seconds

License

BSD License

Parameter

Parameter Description
milliseconds a parameter

Return

String

Declaration

public static String asHoursMinutesSeconds(long milliseconds) 

Method Source Code

//package com.java2s;
/**//from w  w  w  .  j a  va 2 s .co  m
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 */

public class Main {
    /**
     *
     * @param milliseconds
     * @return String
     */
    public static String asHoursMinutesSeconds(long milliseconds) {

        if (milliseconds < 0) {
            throw new IllegalArgumentException();
        }

        long seconds = 0;
        long minutes = 0;
        long hours = 0;

        if (milliseconds > 1000) {
            seconds = milliseconds / 1000;
        }

        if (seconds > 60) {
            minutes = seconds / 60;
            seconds = seconds % 60;
        }

        if (minutes > 60) {
            hours = minutes / 60;
            minutes = minutes % 60;
        }

        StringBuilder res = new StringBuilder();
        if (hours > 0) {
            res.append(hours).append("h ");
        }
        if (hours > 0 || minutes > 0) {
            res.append(minutes).append("m ");
        }
        res.append(seconds).append('s');
        return res.toString();
    }
}

Related

  1. _toBeginOfMinute(long millis)
  2. daysHoursMinutes(long p_milliseconds)
  3. daysHoursMinutesToMillis(String expression)
  4. hoursMinutes(long p_milliseconds)
  5. isLessThanMinute(final long timeInMillis)