Java Time to String timeToString(int time)

Here you can find the source of timeToString(int time)

Description

Transform secunde in format afisabil

License

Open Source License

Parameter

Parameter Description
seconds a parameter

Declaration

public static String timeToString(int time) 

Method Source Code

//package com.java2s;
/*//from  w w  w . ja  va2s.  c  o m
 *
 * This file is part of Genome Artist.
 *
 * Genome Artist 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 3 of the License, or
 * (at your option) any later version.
 *
 * Genome Artist 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 Genome Artist.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    private static final int MINUTES_2_SECONDS = 60;
    private static final int HOURS_2_SECONDS = 3600;

    /**
     * Transform secunde in format afisabil
     * @param seconds
     * @return
     */
    public static String timeToString(int time) {
        String result = "";
        int hours, minutes, seconds;
        int rest;
        String strHour, strMinute, strSecond;
        boolean hasHours = false, hasMinutes = false;

        //Calculez timpi
        hours = time / HOURS_2_SECONDS;
        rest = time % HOURS_2_SECONDS;
        minutes = rest / MINUTES_2_SECONDS;
        seconds = rest % MINUTES_2_SECONDS;

        //Conditiile de afisare
        //HOURS
        if (hours > 0) {
            strHour = hours + "";
            hasHours = true;
        } else {
            strHour = "";
            hasHours = false;
        }
        //MINUTES
        if (hasHours) {
            strMinute = minutes + "";
            hasMinutes = true;
        } else {
            if (minutes > 0) {
                strMinute = minutes + "";
                hasMinutes = true;
            } else {
                strMinute = "";
                hasMinutes = false;
            }
        }

        //SECONDS 
        strSecond = seconds + "";

        //Parsez stringurile si le fac padding cu 0
        if (hasHours)
            strMinute = padWithZero(strMinute, 2);
        if (hasMinutes)
            strSecond = padWithZero(strSecond, 2);

        if (hasHours)
            result += strHour + "h ";
        if (hasMinutes)
            result += strMinute + "m ";
        result += strSecond + "s";

        return result;
    }

    /**
     * Fac padding cu zero la un string
     */
    public static String padWithZero(String string, int count) {
        String padding = "";

        for (int i = string.length(); i < count; i++) {
            padding += "0";
        }

        return padding + string;
    }
}

Related

  1. timeToString(final long time)
  2. timeToString(int s)
  3. timeToString(int time)
  4. timeToString(int time)
  5. TimeToString(int time)
  6. timeToString(java.util.Date date)
  7. timeToString(long time)
  8. timeToString(long time)
  9. timeToString(long time)