Java Calendar Calculate FormatTime(Calendar p_time, boolean p_showMilliseconds)

Here you can find the source of FormatTime(Calendar p_time, boolean p_showMilliseconds)

Description

Format Time

License

Open Source License

Declaration

static public String FormatTime(Calendar p_time, boolean p_showMilliseconds) 

Method Source Code


//package com.java2s;
/*/*  w  w  w  . j av a 2s .  c  o  m*/
   Copyright 2016 Wes Kaylor
    
   This file is part of CoreUtil.
    
   CoreUtil is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
    
   CoreUtil 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 Lesser General Public License for more details.
    
   You should have received a copy of the GNU Lesser General Public License
   along with CoreUtil.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    static public String FormatTime(Calendar p_time, boolean p_showMilliseconds) {
        return FormatTime(p_time.get(Calendar.HOUR_OF_DAY), p_time.get(Calendar.MINUTE),
                p_time.get(Calendar.SECOND), (p_showMilliseconds ? p_time.get(Calendar.MILLISECOND) : -1), ":");
    }

    static public String FormatTime(Calendar p_time, String p_separator) {
        return FormatTime(p_time.get(Calendar.HOUR_OF_DAY), p_time.get(Calendar.MINUTE),
                p_time.get(Calendar.SECOND), -1, p_separator);
    }

    /*********************************
     *
     * @param p_hour
     * @param p_minute
     * @param p_seconds
     * @param p_milliseconds Set this to -1 to turn off display of milliseconds.
     * @return
     */
    static public String FormatTime(int p_hour, int p_minute, int p_seconds, int p_milliseconds) {
        return FormatTime(p_hour, p_minute, p_seconds, p_milliseconds, ":");
    }

    /*********************************
     *
     * @param p_hour
     * @param p_minute
     * @param p_seconds
     * @param p_milliseconds Set this to -1 to turn off display of milliseconds.
     * @param p_separator
     * @return
     */
    static public String FormatTime(int p_hour, int p_minute, int p_seconds, int p_milliseconds,
            String p_separator) {
        StringBuilder t_time = new StringBuilder();

        if (p_hour < 10)
            t_time.append("0");

        t_time.append(p_hour + p_separator);

        if (p_minute < 10)
            t_time.append("0");

        t_time.append(p_minute);

        if (p_seconds >= 0) {
            t_time.append(p_separator);
            if (p_seconds < 10)
                t_time.append("0");

            t_time.append(p_seconds);

            // Optionally add the milliseconds.  We only do this if we added seconds first.
            if (p_milliseconds >= 0) {
                StringBuilder t_milliseconds = new StringBuilder(Integer.toString(p_milliseconds));
                while (t_milliseconds.length() < 3)
                    t_milliseconds.insert(0, "0");

                t_time.append("." + t_milliseconds);
            }
        }

        return t_time.toString();
    }
}

Related

  1. cloneCalendar(Calendar in)
  2. copyCalendar(final Calendar calendar, final boolean stripTime)
  3. decode(String timestamp, Calendar calendar)
  4. encode(Calendar calendar, boolean encodeTzOffset, StringBuilder buf)
  5. equals(Calendar calendar1, Calendar calendar2)
  6. getCalendar(long millis)
  7. getGregorianCalendar(String yyyymmdd)
  8. getLongTime(Calendar cal)
  9. getTimeStr3(Calendar timeInstance)