简体   繁体   English

SpringBoot Thymeleaf序数

[英]SpringBoot Thymeleaf ordinal numbers

I have read a few good posts like this one which explain the method of receiving ordinal numbers when given an int . 我已经阅读了一些像这样的好帖子,它解释了给定int时接收序数的方法。

Now, I have a LocalDate object and I can format my dates using any of the DateTimeFormat patterns in my Thymeleaf template. 现在,我有一个LocalDate对象,我可以使用我的Thymeleaf模板中的任何DateTimeFormat模式格式化我的日期。 Example being something like: 示例如下:

<strong th:text="${item.date} ? ${#temporals.format(item.date, 'dd')}"></strong>

Question: How can I or perhaps what is the best way of achieving similar results to the post I linked to above in Thymeleaf. 问题:我怎样才能或者也许是什么是在Thymeleaf 上面与我联系帖子取得类似结果的最佳方式。

I am not an experienced Java developer so it would be very helpful if you be as thorough as you possibly can with explaining the answer. 我不是一个经验丰富的Java开发人员,所以如果你尽可能彻底地解释答案,那将会非常有帮助。

Inside the Thymeleaf's template you can use static fields (and functions), so in you case it will looks like that: 在Thymeleaf的模板中,您可以使用静态字段 (和函数),因此在您的情况下,它将如下所示:
1) Code from the question you related (I just modified it a little bit) : 1) 你问的相关问题的代码(我刚修改了一下)

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) Call it inside the view : 2)在视图中调用它:

<strong th:text="${#temporals.format(item.date, 'dd') + T(your.packagename.YourClass).getDayOfMonthSuffix(#temporals.format(item.date, 'dd'))}"></strong>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM