How do you format the day of the month to say “11th”, “21st” or “23rd” (ordinal indicator)?

后端 未结 20 965
逝去的感伤
逝去的感伤 2020-11-22 02:41

I know this will give me the day of the month as a number (11, 21, 23):

SimpleDateFormat formatDayOfMonth = new Simple         


        
20条回答
  •  爱一瞬间的悲伤
    2020-11-22 03:08

    Many of the examples here will not work for 11, 12, 13. This is more generic and will work for all case.

    switch (date) {
                    case 1:
                    case 21:
                    case 31:
                        return "" + date + "st";
    
                    case 2:
                    case 22:
                        return "" + date + "nd";
    
                    case 3:
                    case 23:
                        return "" + date + "rd";
    
                    default:
                        return "" + date + "th";
    }
    

提交回复
热议问题