【问题标题】:SpringBoot Thymeleaf ordinal numbersSpring Boot Thymeleaf 序数
【发布时间】:2016-08-24 12:44:09
【问题描述】:

我读过一些像this one 这样的好帖子,其中解释了在给定int 时接收序数的方法。

现在,我有一个 LocalDate 对象,我可以使用 Thymeleaf 模板中的任何 DateTimeFormat 模式来格式化我的日期。示例如下:

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

问题:我怎样才能获得与 Thymeleaf 中的 post I linked to above 类似的结果,或者最好的方法是什么。

我不是一位经验丰富的 Java 开发人员,因此如果您尽可能详尽地解释答案,将会非常有帮助。

【问题讨论】:

    标签: java spring-boot thymeleaf datetime-format


    【解决方案1】:

    在 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>
    

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 2019-06-28
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 2016-01-26
      • 2020-08-16
      • 2019-09-05
      相关资源
      最近更新 更多