4

I tried to use the SimpleDateFormat class to do this, But I did not find any options that put a 'st' after the day. I could only get '31 Dec, 2000'

How to format like "31st Dec, 2000" . I have the date in milliseconds.

Is there any API in java that lets us format a date this way?

Ron
  • 24,175
  • 8
  • 56
  • 97

5 Answers5

14

A simple function with switch case, do this

Public String getDateSuffix( int day) { 
        switch (day) {
            case 1: case 21: case 31:
                   return ("st");

            case 2: case 22: 
                   return ("nd");

            case 3: case 23:
                   return ("rd");

            default:
                   return ("th");
        }
}
Vinesh
  • 933
  • 2
  • 7
  • 22
2

The small function below will return a String suffix. (Stolen from this answer).

String getDayOfMonthSuffix(final int n) {
    if (n < 1 || n > 31) {
        throw new IllegalArgumentException("Illegal day of month");
    }

    if (n >= 11 && n <= 13) {
        return "th";
    }

    switch (n % 10) {
        case 1:  return "st";
        case 2:  return "nd";
        case 3:  return "rd";
        default: return "th";
    }
}

Then, all you need to do is something like:

SimpleDateFormat dd = new SimpleDateFormat("dd");
SimpleDateFormat mmyyyy = new SimpleDateFormat("MMM, yyyy");

String formattedDate = dd.format(date) + getDayOfMonthSuffix(date.get(Calendar.DAY_OF_MONTH)) + " " + mmyyyy.format(date);
Community
  • 1
  • 1
Redandwhite
  • 2,529
  • 4
  • 25
  • 43
  • Yes, the above will work fine... however, it is so bulky in comparison to a single line, but I don't think you can really do it shorter. – Jaco Van Niekerk Sep 10 '12 at 11:51
2

I replied in a comment but I figured I could drop the code.

/**
 * Returns the appropriate suffix from th, nd or rd
 * @param cal
 * @return
 */
public static String dateSuffix(final Calendar cal) {
    final int date = cal.get(Calendar.DATE);
    switch (date % 10) {
    case 1:
        if (date != 11) {
            return "st";
        }
        break;

    case 2:
        if (date != 12) {
            return "nd";
        }
        break;

    case 3:
        if (date != 13) {
            return "rd";
        }
        break;
    }
    return "th";
}

Usage:

SimpleDateFormat sdf = new SimpleDateFormat("d'%s' MMM, yyyy");
String myDate = String.format(sdf.format(date), Util.dateSuffix(date));
Benoit Duffez
  • 11,839
  • 12
  • 77
  • 125
  • How would it be if I already have a date in millis.. – Ron Sep 10 '12 at 11:56
  • I don't know where your date comes from, but I find a lot easier to deal with Java `Calendar` as soon as I need to do stuff with dates. You might want to do: `Calendar cal = new GregorianCalendar(); cal.setTimeInMillis(yourTimeInMillis);` – Benoit Duffez Sep 10 '12 at 11:59
2

This may be a tad shorter:

String getDayOfMonthSuffix(final int n) {
    if (n < 1 || n > 31) {
        throw new IllegalArgumentException("Illegal day of month");
    }
    final String[] SUFFIX = new String[] { "th", "st", "nd", "rd" };
    return (n >= 11 && n <= 13) || (n % 10 > 3) ? SUFFIX[0] : SUFFIX[n % 10];
}
Jaco Van Niekerk
  • 4,180
  • 2
  • 21
  • 48
1

You could use an array of values.

private static final String[] TH_SUFFIX = ",st,nd,rd,th,th,th,th,th,th,th,th,th,th,th,th,th,th,th,th,th,st,nd,rd,th,th,th,th,th,th,th,st".split(",");

public static String getDayOfMonthSuffix(int n) {
    return TH_SUFFIX[n];
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130