Java Time to String timeToString(double time, boolean longNames)

Here you can find the source of timeToString(double time, boolean longNames)

Description

time To String

License

Apache License

Declaration

private static String timeToString(double time, boolean longNames) 

Method Source Code

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

public class Main {
    /** Default negative value */
    public static final String NEGATIVE = "NEGATIVE";

    private static String timeToString(double time, boolean longNames) {
        //      String error = UtilProps.NEGATIVE_VALUE, sign, clock = "%d%s";
        String error = NEGATIVE, clock = "%.2f%s";
        String _s = "s", _m = "m", _h = "h", _d = "d", _w = "w";
        if (longNames) {
            _s = " second(s)";
            _m = " minute(s)";
            _h = " hour(s)";
            _d = " day(s)";
            _w = " week(s)";
        }//from  ww  w.  j av a  2 s  . co  m
        if (time < 0) {
            //         Report.error(TimeUtil.class, String.format("Cannot be negative. Line '%s' was sent instead of '%s'", error, time));
            return error;
        }
        double comparableTime = time / 3600000.0 / 24.0 / 7.0, value;

        if (comparableTime < 1.0) {
            if (comparableTime * 60.0 * 24.0 * 7.0 < 1.0) {
                value = comparableTime * 3600.0 * 24.0 * 7.0;
                return String.format(clock, value, ends(value, _s));
            } else if (comparableTime * 24.0 * 7.0 < 1.0) {
                value = comparableTime * 60.0 * 24.0 * 7.0;
                return String.format(clock, value, ends(value, _m));
            } else if (comparableTime * 7.0 < 1.0) {
                value = comparableTime * 24.0 * 7.0;
                return String.format(clock, value, ends(value, _h));
            } else {
                value = comparableTime * 7.0;
                return String.format(clock, value, ends(value, _d));
            }
        } else {
            value = comparableTime;
            return String.format(clock, value, ends(value, _w));
        }
    }

    /**
     * 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. timeToStr(String time)
  2. timeToString(Calendar date)
  3. timeToString(Date d, String tzString)
  4. timeToString(Date date)
  5. timeToString(double d)
  6. timeToString(final Date date)
  7. timeToString(final long time)
  8. timeToString(int s)
  9. timeToString(int time)