JSF 2.0 + Primefaces 2.x: bind string to calendar

I have a bean that contains a field of type java.lang.String . This field contains free-form text that can represent a number, string, or date. When the field represents the date, I visualize the calendar (performances) on the screen, otherwise the input window will be displayed.

The problem that I am facing is that after the user selects a date through the calendar, I need a date string that will be written in a free form field in order to have a specific format (MM / dd / yyyy). Currently, the string that gets installed has the default format that you get when you execute toString() in the java.util.Date object.

Does anyone know how I can control the format of the string that is being written to my field?

Thanks.

+2
source share
2 answers

I think this should do the following:

 <p:calendar value="#{calTestBean.dateStr}" pattern="MM/dd/yyyy"> <f:convertDateTime pattern="MM/dd/yyyy"/> </p:calendar> 
+1
source

You can use the pattern attribute for calendar lines: pattern="MM/dd/yyyy" , or you can use the java class SimpleDateFormat for the server-side format and data processing (for better control over your string date):

 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); Date myReceivedDate = dateFormat.parse(receivedStringDate); String myNewDate = dateFormat.format(myReceivedDate); 
0
source

Source: https://habr.com/ru/post/1398619/


All Articles