Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the cleanest way to display a java.util.Calendar object in JSF?

Tags:

jsf

calendar

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?

like image 387
Adam Avatar asked Apr 13 '11 17:04

Adam


People also ask

Is Java Calendar deprecated?

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.

Is Calendar thread safe Java?

In short, the Calendar class is not thread-safe, and GregorianCalendar isn't either because it inherits the non-thread-safe fields and methods.

Can you format Calendar class Java?

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.

How do I get Calendar date objects?

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.


2 Answers

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>
like image 60
BalusC Avatar answered Sep 19 '22 19:09

BalusC


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>
&nbsp;
<h:outputText value="#{bean.calendar.time}" styleClass="time">
    <f:convertDateTime pattern="hh:mm" />
</h:outputText>
like image 45
feder Avatar answered Sep 21 '22 19:09

feder