Java Formatter Usage format_hh_mm_ss_Optional(final long value)

Here you can find the source of format_hh_mm_ss_Optional(final long value)

Description

formahms Optional

License

Open Source License

Declaration

public static String format_hh_mm_ss_Optional(final long value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2005, 2015 Wolfgang Schramm and Contributors
 * //from w  w w .  j a v a  2s.  c om
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation version 2 of the License.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *******************************************************************************/

import java.util.Formatter;

public class Main {
    private static final String SYMBOL_DASH = "-";
    private static final String FORMAT_HH_MM = "%d:%02d";
    private static final String FORMAT_HH_MM_SS = "%d:%02d:%02d";
    private static StringBuilder _sbFormatter = new StringBuilder();
    private static Formatter _formatter = new Formatter(_sbFormatter);

    public static String format_hh_mm_ss_Optional(final long value) {

        boolean isShowSeconds = true;
        final int seconds = (int) ((value % 3600) % 60);

        if (isShowSeconds && seconds == 0) {
            isShowSeconds = false;
        }

        String valueText;
        if (isShowSeconds) {

            // show seconds only when they are available

            valueText = format_hh_mm_ss(value);
        } else {
            valueText = format_hh_mm(value);
        }

        return valueText;
    }

    public static String format_hh_mm_ss(final long time) {

        _sbFormatter.setLength(0);

        if (time < 0) {
            _sbFormatter.append(SYMBOL_DASH);
        }

        final long timeAbsolute = time < 0 ? 0 - time : time;

        return _formatter.format(FORMAT_HH_MM_SS, //
                (timeAbsolute / 3600), (timeAbsolute % 3600) / 60, (timeAbsolute % 3600) % 60).toString();
    }

    public static String format_hh_mm(final long time) {

        _sbFormatter.setLength(0);

        if (time < 0) {
            _sbFormatter.append(SYMBOL_DASH);
        }

        final long timeAbsolute = time < 0 ? 0 - time : time;

        return _formatter.format(FORMAT_HH_MM, //
                (timeAbsolute / 3600), (timeAbsolute % 3600) / 60).toString();
    }
}

Related

  1. floatForSql(Float f)
  2. Fmt(String format, Object... args)
  3. format(double value)
  4. format(Locale l, String format, Object... args)
  5. format(String format, Object... args)
  6. format_mm_ss(final long time)
  7. formatLong(long number, int length)
  8. formatMessageException(String format, Object... args)
  9. formatNumber(int number)