java我们如何在下面的格式中设置日期? 1 周,4 日 Questions & Answers 61 日期格式为21^st 实际上,我编写了一个程序,使用简单的日期格式打印我们需求中的日期。但我想用上面的格式打印日期 欢迎大家来解决这个问题
# 1 楼答案 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)]; } }
# 1 楼答案