-1

I need to get the day of month ends with th or st e.g 20th, 21st.

Is it possible to achieve it java date time format? Any kind of help will be highly appreciated. Thanks

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Ehsan Anjum
  • 762
  • 8
  • 15

2 Answers2

2

I think this can do what you want:

public class Base {
    public static void main(String[] args) {
    for (int i = 1; i < 31; i++) {
        System.out.println(i + getOrdinalFor(i));
    }
    }

    public static String getOrdinalFor(int value) {
        int tenRemainder = value % 10;
     if (value == 11 || value == 12 || value == 13) {
        return "th";
     }
     switch (tenRemainder) {
     case 1:
        return "st";
     case 2:
        return "nd";
     case 3:
        return "rd";
     default:
        return "th";
     }
 }
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

check this first where you are using.

if(value <=30){
   getOrdinalFor(value)
}else{
  return "st";
}

here is your core logic

public static String getOrdinalFor(int value) {
 int tenRemainder = value % 20;

 switch (tenRemainder) {
 case 1:
    return "st";
 case 2:
    return "nd";
 case 3:
    return "rd";
 default:
    return "th";
 }
}
Danial Hussain
  • 2,488
  • 18
  • 38