【问题标题】:Spring Java @DateTimeFormatSpring Java @DateTimeFormat
【发布时间】:2012-03-27 08:31:13
【问题描述】:

我创建了一个助手来解析来自 web 服务的时间并以另一种格式输出 使用@DateTimeFormat。但是在我的jsp视图中,这没有格式化。我用错了吗?

 @Component
public class DateParserHelper {

    @DateTimeFormat(style="F-")
    public Date getFormattedDate() {
        return formattedDate;
    }


    public void setFormattedDate(Date formattedDate) {

        this.formattedDate = formattedDate;
    }


    public Date formattedDate;

    public void setDate(String date) throws ParseException {

        DateFormatter dateFormatter = new DateFormatter("y-M-d");
        setFormattedDate( dateFormatter.parse(date, UK));

    }





}

【问题讨论】:

    标签: java datetime spring-mvc


    【解决方案1】:

    如果你在你的 jsp 中使用 JSTL,你可以使用 formatDate 来格式化你的日期:

    <fmt:formatDate value="${yourDate}" pattern="dd/MM/yyyy" />
    

    【讨论】:

    • 我希望日期以特定格式呈现给视图。但我会试一试,谢谢。
    • 在仍然无法让@dateTimeFormat 工作之后,我同意了,谢谢
    【解决方案2】:

    试试SimpleDateFormat

    String pattern = "MM/dd/yyyy";
    
    ...
    
    public void setDate(String date) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        setFormattedDate(format.parse(date));
    }
    

    【讨论】:

    • 谢谢 - 尽管我知道 SimpleDateFORMAT,但我希望保留 Spring 的东西
    猜你喜欢
    • 2018-10-12
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2023-01-19
    • 2011-12-22
    • 2021-06-25
    相关资源
    最近更新 更多