SpringBoot Thymeleaf 序号

SpringBoot Thymeleaf ordinal numbers

我读过一些像 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>