我已经阅读了this one这样的好帖子,它们解释了在给定int
时接收序数的方法。
现在,我有了一个LocalDate对象,并且可以使用Thymeleaf模板中的任何DateTimeFormat
模式格式化日期。示例如下:
<strong th:text="${item.date} ? ${#temporals.format(item.date, 'dd')}"></strong>
问题:我如何或者也许是达到与Thymeleaf中post I linked to above类似结果的最佳方法是什么?
我不是一位经验丰富的Java开发人员,所以如果您尽可能详尽地解释答案,它将对您很有帮助。
最佳答案
在Thymeleaf的模板中,您可以use static fields(和函数),因此在您的情况下,它看起来像这样:
1)Code from the question you related (I just modified it a little bit):
package your.packagename;
// http://code.google.com/p/guava-libraries
import static com.google.common.base.Preconditions.*;
public class YourClass {
public static String getDayOfMonthSuffix(String num) {
Integer n = Integer.valueOf(num == null ? "1" : num);
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";
}
}
}
2)在视图内部调用它:
<strong th:text="${#temporals.format(item.date, 'dd') + T(your.packagename.YourClass).getDayOfMonthSuffix(#temporals.format(item.date, 'dd'))}"></strong>