2016-04-29 213 views
2

我已经阅读了一些好帖子,如this one,它解释了在给定int时接收序号的方法。SpringBoot Thymeleaf序号

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

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

问:哪有我或许什么是实现了类似的结果在Thymeleaf的post I linked to above的最佳途径。

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

回答

2

里面的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>