Java Calendar Format FormatDate(Calendar p_date, String p_separator)

Here you can find the source of FormatDate(Calendar p_date, String p_separator)

Description

This is a wrapper to convert calls from existing code to the new FormatData() that takes the p_dateOrderType parameter.

License

Open Source License

Declaration

static public String FormatDate(Calendar p_date, String p_separator) 

Method Source Code


//package com.java2s;
/*// w  ww  .j  a v  a2  s  .  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;

    /*********************************
     * 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();
    }
}

Related

  1. formatDate(Calendar calendar)
  2. formatDate(Calendar date)
  3. formatDate(Calendar date)
  4. formatDate(Calendar date)
  5. formatDate(Calendar date, int precision)
  6. formatDate(String formats, Calendar date)
  7. formatDateTime(Calendar cal)
  8. formatDateTimeUTC(Calendar datetime)
  9. formatDay(java.util.Calendar d)