Java Utililty Methods Date String Format

List of utility methods to do Date String Format

Description

The list of methods to do Date String Format are organized into topic(s).

Method

StringdateFormat(String date)
date Format
if (date.length() == 10) {
    return date.substring(0, 2) + "-" + date.substring(2, 4) + ' ' + date.substring(4, 6) + ':'
            + date.substring(6, 8) + ':' + date.substring(8, 10);
} else if (date.length() == 8) {
    return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
} else if (date.length() == 6) {
    return date.substring(0, 2) + ":" + date.substring(2, 4) + ":" + date.substring(4, 6);
} else if (date.length() == 4) {
...
StringdateFormat(String datetime)
date Format
String date = "";
if (datetime != null && !"null".equals(datetime.trim()) && !"".equals(datetime.trim())
        && datetime.length() > 10) {
    if ("00:00:00".equals(datetime.substring(11, 19))) {
        date = datetime.substring(0, 10);
    } else {
        date = datetime.substring(0, 19);
} else {
    date = datetime;
return date;
StringdateFormat(String strYear, String strMonth, String strDay)
date Format
if (strYear == null || strYear.equals(""))
    return "";
if (strMonth == null || strMonth.equals(""))
    return "";
if (strDay == null || strDay.equals(""))
    return "";
strYear = strYear.trim();
strMonth = strMonth.trim();
...
StringdateFormatFix(String str)
date Format Fix
if (str.indexOf("M") != -1 && str.indexOf("MM") == -1 && str.indexOf("MMM") == -1) {
    str = str.replaceAll("M", "MM");
if (str.indexOf("d") != -1 && str.indexOf("dd") == -1 && str.indexOf("ddd") == -1) {
    str = str.replaceAll("d", "dd");
if (str.indexOf("y") != -1 && str.indexOf("yy") == -1 && str.indexOf("yyy") == -1) {
    str = str.replaceAll("y", "yy");
...
StringformatDate(byte[] btValue, int iOffset, int iLength)
format Date
if ((btValue.length < iOffset + iLength) || (iLength < 4))
    return "";
return getBCDString(btValue[iOffset + 3]) + "/" + getBCDString(btValue[iOffset + 2]) + "/"
        + getBCDString(btValue[iOffset + 0]) + getBCDString(btValue[iOffset + 1]);
StringformatDate(int year, int month, int day)
format Date
return String.format("%d-%02d-%02d", year, month, day);
StringformatDate(Integer date)
format Date
try {
    return (date + "").substring(0, 4) + "-" + (date + "").substring(4, 6) + "-"
            + (date + "").substring(6, 8);
} catch (Exception e) {
    e.printStackTrace();
    return "";
StringformatDate(Integer day, Integer month, Integer year)
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.
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) {
...
StringFormatDate(String aDate)
Change the separator of the date
String[] lParts = aDate.split("\\.", 0);
return String.format("%s-%s-%s", lParts[0], lParts[1], lParts[2]);
StringformatDate(String date)
format Date
final String template = "2010-01-01T00:00:00Z";
final int tl = template.length();
final int dl = date.length();
if (dl > tl)
    return date.substring(0, tl - 1) + "Z"; 
else if (dl < tl)
    return date + template.substring(dl);
return date;
...