我有域类:
public class Note
{
private Date date;
// other fields;
public void setDate( Date date )
{
this.date = date;
}
public Date getDate ()
{
return date;
}
}
但在 Thymeleaf 中我不想直接使用字段日期。通常我可以有一种方法,比如
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)}
关于java - Thymeleaf - :text 中的类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37225091/