我有一个像这样的 String
:2013-04-19
,我想把它改成:April 19th 2013
.我知道 Java 中有一些类,例如 SimpleDateFormat
,但我不知道应该使用哪种函数。也许我需要选择 Pattern
类?我需要一些帮助。
最佳答案
尝试以下操作,这应该会为您提供几天的正确“th”、“st”、“rd”和“nd”。
public static String getDayOfMonthSuffix(final int n) {
if (n >= 11 && n <= 13) {
return "th";
}
switch (n % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
public static void main(String[] args) throws ParseException {
Date d = new SimpleDateFormat("yyyy-MM-dd").parse("2013-04-19");
int day = Integer.parseInt(new java.text.SimpleDateFormat("dd").format(d));
SimpleDateFormat sdf = new SimpleDateFormat("MMMMM dd'" + getDayOfMonthSuffix(day) + "' yyyy");
String s = sdf.format(d);
System.out.println(s);
}
这将打印 April 19th 2013
(日终止改编自 this post )
关于java - 将字符串日期更改为另一个字符串日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14308148/