2016-10-18 4 views
2

This is my data modelタイムリーフ+春の日付変換

これは私のデータモデルです。私はここから日付を使いたい。

私は私のhtmlでこれを行う:

<table th:if="${!commentsInTask.empty}"> 

    <tbody> 
    <tr th:each="Comments : ${commentsInTask}"> 
    <tr th:each="comment : ${Comments}"> 
     <td th:text="${comment.user}">user ...</td> 

     <td th:text="${comment.createdAt}">date ...</td> 
    </tr> 
    </tr> 
    </tbody> 
</table> 

それがもたらす:

<table> 

    <tbody> 
     <td>JACK</td> 

     <td>1.476787930289E9</td> 
    </tr> 
    </tr> 
    </tbody> 
</table> 

この部分は、UNIXのtimedateです: 1.476787930289E9

しかし、私が投稿先頭の画像に、 あなたが見た。ティムメはそうではありません。 Canttのは、私が最初のピクチャ内のどの日付形式で表示さなぜ

これは、ドメイン内

public String getCreatedAtString() { 
     return createdAtString; 
    } 

    public TaskComment setCreatedAtString(String createdAtString) { 
     this.createdAtString = createdAtString; 
     return this; 
    } 
private ZonedDateTime createdAt = ZonedDateTime.now(); 

のですか?

答えて

4

使用Thymeleafのフォーマットは:

<td th:text="${#dates.format(comment.createdAt, 'dd-MM-yyyy HH:mm:ss')}">date</td> 

次の形式で出力が得られます:18-Oct-2016 14:44:05

#datesjava.util.Dateオブジェクトのための方法:書式設定、成分抽出など


java.util.Dateタイプの使用にごcreatedAtフィールドを変換するには:

Date date = Date.from(java.time.ZonedDateTime.now().toInstant()); 

それともjava.util.Dateタイプを使用します。

private Date createdAt = new Date(); 

これはcheatedAtを現在の日付に設定します。


また、あなたはあなたのZonedDateTimeタイプで動作するようにあなたのプロジェクトにthymeleaf-extras-java8time依存関係を追加することができます。

このモジュールは、Thymeleafのテンプレートからの一時的なオブジェクトの書式設定と作成ができるように、標準的な方言で#datesまたは#calendarsものと同様#temporalsオブジェクトを追加します。

enter image description here

を次にあなたがZoneDateTimeを使用することができます指定パターン:

${#temporals.format(temporal, 'dd/MM/yyyy HH:mm')} 

詳細はThymeleaf - Module for Java 8 Time API compatibilityを参照してください。

+0

18/10/2016 13:51 w hat about this? – mark

+1

'dd/MM/YYYY HH:mm'になります。そこに任意のパターンを使用できます。詳細はこちらをご覧ください:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – DimaSan

+0

org.thymeleaf.exceptions.TemplateProcessingException:例外の評価SpringEL式: "#dates.format(comment.createdAt、 'dd/MM/YYYY HH:mm')"(task_comment_cta:33) – mark