th:text中的Thymelaf类方法



我有域类:

public class Note
{
    private Date date;
    // other fields;
    public void setDate( Date date )
    {
        this.date = date;
    }
    public Date getDate ()
    {
        return date;
    }
}

但在Thymelaf中,我不想直接使用字段日期。通常我可以有一种方法,比如

public String getFormattedDate()
{
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    return dateFormat.format(date);
}

没有名为formattedDate的字段(我不需要它)。

但是。。我做不到:

th:text = "${note.formattedDate}"

我可以添加一个名为formattedDate+getters和setters的字段,但在我看来,这是一个非常糟糕的解决方案,因为我只需要格式化日期的值。

你有解决这个问题的办法吗?

最好的解决方案是使用

${#dates.format(date, YOUR_PATTERN, #locale)} 

最新更新