Java Time to String timeToString(int time)

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

Description

Converts seconds to a more friendly strings.

License

Open Source License

Parameter

Parameter Description
time Time in seconds

Return

A friendly string, which converts the seconds into days, hours, minutes, and seconds.

Declaration

public static String timeToString(int time) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from  w w  w . jav  a2s  .c  o  m
     * Converts seconds to a more friendly strings. Will convert up to days.
     * 
     * @param time
     *            Time in seconds
     * @return A friendly string, which converts the seconds into days, hours,
     *         minutes, and seconds.
     */
    public static String timeToString(int time) {
        String message = "";
        if (time > 60) {
            int minutes = (int) Math.floor(time / 60), seconds = time - (minutes * 60);
            if (minutes > 60) {
                int hours = (int) Math.floor(minutes / 60);
                minutes -= (hours * 60);
                if (hours > 24) {
                    int days = (int) Math.floor(hours / 24);
                    message += days + " day";
                    if (days > 1) {
                        message += "s";
                    }
                    message += ", ";
                    hours -= (days * 24);
                }
                message += hours + " hour";
                if (hours > 1) {
                    message += "s";
                }
                message += ", ";
            }
            message += minutes + " minute";
            if (minutes > 1) {
                message += "s";
            }
            if (seconds > 0) {
                message += " and " + (seconds) + " seconds";
            }
        } else {
            message += time + " seconds";
        }
        return message;
    }
}

Related

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