Java Time to String timeToFullString(long time, boolean longNames)

Here you can find the source of timeToFullString(long time, boolean longNames)

Description

time To Full String

License

Apache License

Declaration

private static String timeToFullString(long time, boolean longNames) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static String timeToFullString(long time, boolean longNames) {

        long s, m, h, d, w;
        String line = "", _s = "s ", _m = "m ", _h = "h ", _d = "d ", _w = "w ", clock = "%d%s";
        if (longNames) {
            _s = " second(s) ";
            _m = " minute(s) ";
            _h = " hour(s) ";
            _d = " day(s) ";
            _w = " week(s) ";
        }//from   ww  w. ja  v a2  s. c o m

        w = time / 3600000 / 24 / 7;
        d = (time / 3600000 / 24) % 7;
        if (w > 0) {
            line += String.format(clock, w, ends(w, _w));
        }
        if (d > 0) {
            line += String.format(clock, d, ends(d, _d));
        }
        h = (time / 3600000) % 24;
        if (h > 0) {
            line += String.format(clock, h, ends(h, _h));
        }
        m = (time / 60000) % 60;
        if (m > 0) {
            line += String.format(clock, m, ends(m, _m));
        }
        s = (time / 1000) % 60;
        if (s > 0) {
            line += String.format(clock, s, ends(s, _s));
        }
        if (line.equals("")) {
            return "0s";
        }

        return line.trim();
    }

    /**
     * Prints formatted string with no value included. Sets the ending using given text and inspecting the value.
     * 
     * @param value Number to check and set proper ending
     * @param text Text for <b>value</b> measuring unit singular and plural forms. Where plural form should be separated from singular by using <i>(s)</i> or
     * <i>(es)</i> and the end ("<i>line(s)</i>" as an example).
     * @return Formatted string with no value included, if success
     */
    public static String ends(long value, String text) {
        return convertEnd(value, text, false);
    }

    /**
     * Prints formatted string with no value included. Sets the ending using given text and inspecting the value.
     * 
     * @param value Number to check and set proper ending
     * @param text Text for <b>value</b> measuring unit singular and plural forms. Where plural form should be separated from singular by using <i>(s)</i> or
     * <i>(es)</i> and the end ("<i>line(s)</i>" as an example).
     * @return Formatted string with no value included, if success
     */
    public static String ends(double value, String text) {
        return convertEnd((long) value, text, false);
    }

    private static String convertEnd(long value, String text, boolean incNumber) {

        String end = "";
        if (text.contains("(s)")) {
            long hundred = value % 100, ten = value % 10;

            if ((ten == 1) && (hundred != 11)) {
                end = text.replace("(s)", "");
            } else {
                end = text.replace("(s)", "s");
            }
        } else if (text.contains("(es)")) {
            long hundred = value % 100, ten = value % 10;

            if ((ten == 1) && (hundred != 11)) {
                end = text.replace("(es)", "");
            } else {
                end = text.replace("(es)", "es");
            }
        } else {
            end = text;
        }
        if (incNumber)
            end = String.format("%d %s", value, end.trim());

        return end;
    }
}

Related

  1. timeString(double time)
  2. timeString(long tickNo, long offset)
  3. timeString2Date(String time)
  4. timeToDeparture(String startTime)
  5. timeToFullString(long time)
  6. timeToStr(int time)
  7. timeToStr(String time)
  8. timeToString(Calendar date)
  9. timeToString(Date d, String tzString)