0

The Date format is 21^st

Actually i written a program to print date in our requirement by using simple Date Format. But I want to print Date in the above formate

welcome to all to solve this problem

Alex K
  • 22,315
  • 19
  • 108
  • 236
k manohar
  • 47
  • 2
  • 8
  • http://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-in-java – Igor Vuković Dec 21 '15 at 06:54
  • Possible duplicate of [How do you format the day of the month to say "11th", "21st" or "23rd" in Java? (ordinal indicator)](http://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-in-java) – Alex K Jan 20 '17 at 08:19

1 Answers1

2
public class Test {
    public static void main(String[] args) {
        SimpleDateFormat formatDayOfMonth = new SimpleDateFormat("d");
        String s = formatDayOfMonth.format(new Date());
        System.out.println(s);
        int num=Integer.parseInt(s);
        System.out.println(get(num));

    }

    private static String get(int num){
        String[] suffix = {"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"};
        int m = num % 100;
       return String.valueOf(num) + suffix[(m > 10 && m < 20) ? 0 : (m % 10)];
    }
}
soorapadman
  • 4,451
  • 7
  • 35
  • 47