Java Time to String timeToString(long time)

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

Description

time To String

License

Open Source License

Declaration

public static String timeToString(long time) 

Method Source Code

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

public class Main {
    private static long second = 1000, minute = 60000, hour = 3600000, day = 86400000,
            month = Long.valueOf("2592000000"), year = Long.valueOf("31104000000");

    public static String timeToString(long time) {
        long timeYears = time > year ? time / year : 0;
        time -= timeYears * year;/*from w w w.  jav a2  s .c  o  m*/
        long timeMonths = time > month ? time / month : 0;
        time -= timeMonths * month;
        long timeDays = time > day ? time / day : 0;
        time -= timeDays * day;
        long timeHours = time > hour ? time / hour : 0;
        time -= timeHours * hour;
        long timeMinutes = time > minute ? time / minute : 0;
        time -= timeMinutes * minute;
        long timeSeconds = time > second ? time / second : 0;
        time -= timeSeconds * second;

        String returnableString = "";

        returnableString += timeYears != 0 ? timeYears + " " + (timeYears > 1 ? "Jahre" : "Jahr") + " " : "";
        returnableString += timeMonths != 0 ? timeMonths + " " + (timeMonths > 1 ? "Monate" : "Monat") + " " : "";
        returnableString += timeDays != 0 ? timeDays + " " + (timeDays > 1 ? "Tage" : "Tag") + " " : "";

        returnableString += timeHours != 0 ? timeHours + " " + (timeHours > 1 ? "Stunden" : "Stunde") + " " : "";
        returnableString += timeMinutes != 0 ? timeMinutes + " " + (timeMinutes > 1 ? "Minuten" : "Minute") + " "
                : "";
        returnableString += timeSeconds != 0 ? timeSeconds + " " + (timeSeconds > 1 ? "Sekunden" : "Sekunde") + " "
                : "";

        if (returnableString.length() == 0)
            returnableString = "< 1 " + "Sekunden";
        return returnableString.trim();
    }
}

Related

  1. timeToString(int time)
  2. timeToString(int time)
  3. TimeToString(int time)
  4. timeToString(java.util.Date date)
  5. timeToString(long time)
  6. timeToString(long time)
  7. timeToString(long time)
  8. toString(Calendar modifiedTime, String pattern)
  9. toString(Date d, boolean includeTime)