我读过一些不错的帖子,比如 this one它解释了在给定 int
时接收序数的方法。
现在,我有一个 LocalDate 对象,我可以使用我的 Thymeleaf 模板中的任何 DateTimeFormat
模式来格式化我的日期。例如:
<strong th:text="${item.date} ? ${#temporals.format(item.date, 'dd')}"></strong>
问题:我怎样才能或者什么是实现与 post I linked to above 类似结果的最佳方法?在 thymeleaf 中。
我不是经验丰富的 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) 在 View 中调用它:
<strong th:text="${#temporals.format(item.date, 'dd') + T(your.packagename.YourClass).getDayOfMonthSuffix(#temporals.format(item.date, 'dd'))}"></strong>
关于java - Spring Boot Thymeleaf 序号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36936910/