Java Time Format format(long time, String timerPrecisionId, boolean round)

Here you can find the source of format(long time, String timerPrecisionId, boolean round)

Description

format

License

Open Source License

Declaration

public static String format(long time, String timerPrecisionId,
            boolean round) 

Method Source Code

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

public class Main {
    public static String format(long time, String timerPrecisionId,
            boolean round) {
        if (-60000 < time && time < 60000) {
            return formatSeconds(time, timerPrecisionId, round);
        }//  w ww  . ja va 2s . c om

        String result = "";

        if (time == Long.MAX_VALUE) {
            return "DNF";
        }

        String sign = "";
        if (time < 0) {
            sign = "-";
            time = -time;
        }

        if (timerPrecisionId.equals("CENTISECONDS")) {
            if (round) {
                time = time + 5;
            }
            time = time / 10;

            long minutes = time / 6000;
            long seconds = (time / 100) % 60;
            long centiseconds = time % 100;
            result = sign
                    + minutes
                    + ":"
                    + (seconds < 10 ? "0" + seconds : seconds)
                    + "."
                    + (centiseconds < 10 ? "0" + centiseconds
                            : centiseconds);
        } else if (timerPrecisionId.equals("MILLISECONDS")) {
            long minutes = time / 60000;
            long seconds = (time / 1000) % 60;
            long milliseconds = time % 1000;

            result = sign
                    + minutes
                    + ":"
                    + (seconds < 10 ? "0" + seconds : seconds)
                    + "."
                    + (milliseconds < 10 ? "00" + milliseconds
                            : (milliseconds < 100 ? "0" + milliseconds
                                    : milliseconds));
        }

        return result;
    }

    public static String formatSeconds(long time, String timerPrecisionId,
            boolean round) {
        String result = "";

        if (time == Long.MAX_VALUE) {
            return "DNF";
        }

        String sign = "";
        if (time < 0) {
            sign = "-";
            time = -time;
        }

        if (timerPrecisionId.equals("CENTISECONDS")) {
            if (round) {
                time = time + 5;
            }
            time = time / 10;

            long seconds = time / 100;
            long centiseconds = time % 100;
            result = sign
                    + seconds
                    + "."
                    + (centiseconds < 10 ? "0" + centiseconds
                            : centiseconds);
        } else if (timerPrecisionId.equals("MILLISECONDS")) {
            long seconds = time / 1000;
            long milliseconds = time % 1000;
            result = sign
                    + seconds
                    + "."
                    + (milliseconds < 10 ? "00" + milliseconds
                            : (milliseconds < 100 ? "0" + milliseconds
                                    : milliseconds));
        }

        return result;
    }
}

Related

  1. calFormatTime(final Calendar cal)
  2. compareTime(String time1, String time2, String dateFormat)
  3. createTimeFormatter(String languageCode)
  4. format(long time)
  5. format(long time)
  6. format(String format, long time)
  7. formatBenchResult(final String benchName, final int iterationCount, final int benchNumber, final double benchTime)
  8. formatCooldown(long time)
  9. formatDateTime(String DateTimeStr)