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
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
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";
}
}
}
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";
}
}