What is the cleanest way to display a java.util.Calendar object in JSF?
We开发者_如何学JAVA 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?
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 need futher Text:
<h:outputFormat
value="#This my date: {0,date,long}">
<f:param value="#{bean.calendar.time}" />
</h:outputFormat>
see: https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html
精彩评论