Java Date String Format formatDate(Integer day, Integer month, Integer year)

Here you can find the source of formatDate(Integer day, Integer month, Integer year)

Description

Get a date in the form MonthDayYear where Mmm is a 3-letter month abbrev, day is of the form Dth and year is of the form YYYY (where D and Y are digits and th is the appropriate number extension for the day.

License

Open Source License

Parameter

Parameter Description
day day of month (1-based)
month month (0-based, January=0)
year 4-digit year

Declaration

public static final String formatDate(Integer day, Integer month, Integer year) 

Method Source Code

//package com.java2s;
/*/* w w w .  j  a va2  s  .co  m*/
Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
This file is part of the Semantic Discovery Toolkit.
    
The Semantic Discovery Toolkit 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.
    
The Semantic Discovery Toolkit 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 The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
*/

public class Main {
    public static final String[] MONTH_ABBREVS = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
            "Aug", "Sep", "Oct", "Nov", "Dec", };
    public static final String[] NUMBER_SUFFIX = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th",
            "th", "th", };

    /**
     * Get a date in the form MonthDayYear where Mmm is a 3-letter month abbrev,
     * day is of the form Dth and year is of the form YYYY (where D and Y are
     * digits and th is the appropriate number extension for the day.
     *
     * @param day    day of month (1-based)
     * @param month  month (0-based, January=0)
     * @param year   4-digit year
     */
    public static final String formatDate(Integer day, Integer month, Integer year) {
        final StringBuilder result = new StringBuilder();

        if (month != null) {
            result.append(MONTH_ABBREVS[month]);
        }
        if (day != null) {
            result.append(Integer.toString(day)).append(getNumberSuffix(day));
        }
        if (year != null) {
            result.append(asDigits(year, 4));
        }

        return result.toString();
    }

    public static final String getNumberSuffix(int number) {
        final int mod100 = number % 100;
        return (mod100 >= 10 && mod100 <= 20) ? "th" : NUMBER_SUFFIX[(number % 10)];
    }

    public static final String asDigits(int number, int minNumDigits) {
        final StringBuilder result = new StringBuilder();

        result.append(Integer.toString(number));
        final int len = result.length();
        for (int i = minNumDigits - len; i > 0; --i)
            result.insert(0, '0');

        return result.toString();
    }
}

Related

  1. dateFormat(String strYear, String strMonth, String strDay)
  2. dateFormatFix(String str)
  3. formatDate(byte[] btValue, int iOffset, int iLength)
  4. formatDate(int year, int month, int day)
  5. formatDate(Integer date)
  6. FormatDate(String aDate)
  7. formatDate(String date)
  8. formatDate(String date)
  9. formatDate(String date)