2016-04-29 217 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>