Java Milliseconds FormatDateTime(Calendar p_date, String p_seperator, boolean p_showMilliseconds)

Here you can find the source of FormatDateTime(Calendar p_date, String p_seperator, boolean p_showMilliseconds)

Description

Format Date Time

License

Open Source License

Declaration

static public String FormatDateTime(Calendar p_date, String p_seperator, boolean p_showMilliseconds) 

Method Source Code


//package com.java2s;
/*/* ww w  . ja  v 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 final int DATE_ORDER_AMERICAN = 0;
    static public final int DATE_ORDER_BRITISH = 1;
    static public final int DATE_ORDER_NUMERIC = 2;

    static public String FormatDateTime(Calendar p_date, String p_seperator, boolean p_showMilliseconds) {
        StringBuilder t_dateTime = new StringBuilder();

        t_dateTime.append(FormatDate(p_date, p_seperator) + " ");
        t_dateTime.append(FormatTime(p_date.get(Calendar.HOUR_OF_DAY), p_date.get(Calendar.MINUTE),
                p_date.get(Calendar.SECOND), (p_showMilliseconds ? p_date.get(Calendar.MILLISECOND) : -1)));

        return t_dateTime.toString();
    }

    static public String FormatDateTime(Calendar p_date, String p_seperator, int p_dateOrderType) {
        StringBuilder t_dateTime = new StringBuilder();

        t_dateTime.append(FormatDate(p_date, p_seperator, p_dateOrderType) + " ");
        t_dateTime.append(FormatTime(p_date.get(Calendar.HOUR_OF_DAY), p_date.get(Calendar.MINUTE),
                p_date.get(Calendar.SECOND), -1));

        return t_dateTime.toString();
    }

    /*********************************
     * This is a wrapper to convert calls from existing code to the new FormatData() that takes the p_dateOrderType parameter.
     * All of the existing code uses the YYYYMMDD ordering, so all we have to do here is default to DATE_ORDER_NUMERIC.
     */
    static public String FormatDate(Calendar p_date, String p_separator) {
        return FormatDate(p_date, p_separator, DATE_ORDER_NUMERIC);
    }

    static public String FormatDate(Calendar p_date, String p_separator, int p_dateOrderType) {
        int t_dateValue;
        StringBuilder t_todaysDate = new StringBuilder();
        StringBuilder t_year = new StringBuilder(Integer.toString(p_date.get(Calendar.YEAR)));
        StringBuilder t_month = new StringBuilder();
        StringBuilder t_day = new StringBuilder();

        t_dateValue = p_date.get(Calendar.MONTH) + 1;
        if (t_dateValue < 10)
            t_month.append("0");

        t_month.append(Integer.toString(t_dateValue));

        t_dateValue = p_date.get(Calendar.DAY_OF_MONTH);
        if (t_dateValue < 10)
            t_day.append("0");

        t_day.append(Integer.toString(t_dateValue));

        switch (p_dateOrderType) {
        case DATE_ORDER_AMERICAN:
            t_todaysDate.append(t_month + p_separator + t_day + p_separator + t_year);
            break;
        case DATE_ORDER_BRITISH:
            t_todaysDate.append(t_day + p_separator + t_month + p_separator + t_year);
            break;
        case DATE_ORDER_NUMERIC:
            t_todaysDate.append(t_year + p_separator + t_month + p_separator + t_day);
            break;
        }
        return t_todaysDate.toString();
    }

    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. elapsedMillis(long t0nanos, long t1nanos)
  2. ensureSleepMillis(long millis)
  3. expensiveMethodTakingMillis(final int millis)
  4. filetimeToMillis(long filetime)
  5. formatDateMillis(long millis)
  6. fractionOfDayToMilliseconds(float fFractionOfDay)
  7. getDurationFromMillis(final long millis)
  8. getDurationSecFromMillisDouble(long millis)
  9. getElapsedTimeInMilliseconds(Date startDate, Date endDate)