How do you format the day of the month to say quot;11thquot;, quot;21stquot; or quot;23rdquot; (ordinal indicator)?(你如何格式化一个月中的一天来说“11th,“21st?或“23日(序数指标)?)
问题描述
我知道这会给我一个月中的日期作为数字(11
、21
、23
):
I know this will give me the day of the month as a number (11
, 21
, 23
):
SimpleDateFormat formatDayOfMonth = new SimpleDateFormat("d");
但是如何格式化一个月中的一天以包含 序数指标,说 11th
、21st
还是 23rd
?
But how do you format the day of the month to include an ordinal indicator, say 11th
, 21st
or 23rd
?
推荐答案
// https://github.com/google/guava
import static com.google.common.base.Preconditions.*;
String getDayOfMonthSuffix(final int n) {
checkArgument(n >= 1 && n <= 31, "illegal day of month: " + 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";
}
}
来自@kaliatech 的表格很不错,但由于重复了相同的信息,它为错误打开了机会.在 7tn
、17tn
和 27tn
的表中确实存在这样的错误(此错误可能会随着时间的推移而得到修复,因为流动性StackOverflow 的性质,因此请检查答案上的版本历史以查看错误).
The table from @kaliatech is nice, but since the same information is repeated, it opens the chance for a bug. Such a bug actually exists in the table for 7tn
, 17tn
, and 27tn
(this bug might get fixed as time goes on because of the fluid nature of StackOverflow, so check the version history on the answer to see the error).
这篇关于你如何格式化一个月中的一天来说“11th",“21st"?或“23日"(序数指标)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:你如何格式化一个月中的一天来说“11th",“21st"?或“23日"(序数指标)?


基础教程推荐
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01