开发者

Problem with Date format using an attribute Calendar with JPA

I've a problem using an attribute Calendar with JPA:

@Column(name = "Date")
@Temporal(TemporalType.DATE)
private Calendar date;

B开发者_JAVA技巧y default it is setted as mm/dd/yyyy and I need it in dd/mm/yyyy, can anyone help me?

I've seen that is quiete easy with Date instead of Calendar but i can't use it...

Thx Daniele


I think, format doesn't really matter. Neither Calendar nor Date have any format with them. Its the toString() method that formats it in the default format.

Would you care to elaborate,

  • what is the expected behaviour?
  • what you are getting?

[Edited based on the comment below]

Whatever, comes from the browser it comes as a String. So, you are having an issue while converting that String to the Date or Calendar. Fix that and all will be good after that.

Happy coding. Cheers.


I believe this is not a JPA issue but SQL configuration.
From what you're describing, your SQL server is configured to default format. You should change it to the format you need.

Take a look on SQL date formats - http://www.sql-server-helper.com/tips/date-formats.aspx


If this is the case, why not you can try SimpleDateFormatter, where you can parse the date and using dateFormatter you can get the desired date format.For more clarification please try the link.


Try the following and let us know:

        String formattedDate = "";
        String requiredFormat = "MM/dd/yyyy";
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
        Date dt = formatter.parse("13/09/2011");
        DateFormat reqFormatter = new SimpleDateFormat(requiredFormat, Locale.ENGLISH);  
        formattedDate = reqFormatter.format(dt);
        System.out.println("Output Date: " + formattedDate);

//formatted Date will give you what expects to be there in DB.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜