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日"(序数指标)?


基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01