簡體   English   中英

SpringBoot Thymeleaf序數

[英]SpringBoot Thymeleaf ordinal numbers

我已經閱讀了一些像這樣的好帖子,它解釋了給定int時接收序數的方法。

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

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

問題:我怎樣才能或者也許是什么是在Thymeleaf 上面與我聯系帖子取得類似結果的最佳方式。

我不是一個經驗豐富的Java開發人員,所以如果你盡可能徹底地解釋答案,那將會非常有幫助。

在Thymeleaf的模板中,您可以使用靜態字段 (和函數),因此在您的情況下,它將如下所示:
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)在視圖中調用它:

<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