We have an object with java.util.Calendar
objects. We would like to display the data on a JSF page (preferably in the same format we have for java.util.Date
objects). It seems like there should be some clean way to do this other than creating a wrapper class just to convert the Calendar to a Date.
What is the cleanest way to display the date/time held in a java.util.Calendar
in a JSF page?
Date are deprecated since Java 1.1, the class itself (and java. util. Calendar , too) are not officially deprecated, just declared as de facto legacy. The support of the old classes is still important for the goal of backwards compatibility with legacy code.
In short, the Calendar class is not thread-safe, and GregorianCalendar isn't either because it inherits the non-thread-safe fields and methods.
The DateFormat class in Java is used for formatting dates. A specified date can be formatted into the Data/Time string. For example, a date can be formatted into: mm/dd/yyyy.
getInstance() method the date from calendar object is obtained and using getTime() method it is converted to Date object. Date object to Calendar object, Date d=new Date(1515660075000l); The above code is used to get date and time from date object.
Use Calendar
's own getter Calendar#getTime()
. It returns a Date
. Then you can use <f:convertDateTime>
the usual way.
<h:outputText value="#{bean.calendar.time}">
<f:convertDateTime type="both" dateStyle="long" />
</h:outputText>
For others and @seangates: You CAN apply a pattern if using the Calendar object. E.g.
<h:outputText value="#{bean.calendar.time}" styleClass="date">
<f:convertDateTime pattern="EEEE, MMMM d yyyy" />
</h:outputText>
<h:outputText value="#{bean.calendar.time}" styleClass="time">
<f:convertDateTime pattern="hh:mm" />
</h:outputText>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With