2016-04-29 19 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開発者ではありませんので、解答の説明に徹底的に理解していただければ非常に役に立ちます。あなたはuse static fields(および機能)をすることができますThymeleafのテンプレートインサイド

答えて

2

、あなたにそれがそのように見えるだろうと小文字ので:
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> 
関連する問題