Format Date output in JSF

后端 未结 3 833
失恋的感觉
失恋的感觉 2020-11-28 11:08

If #{myBean.birthdate} is of java.util.Calendar or java.util.Date type, can I possibly format this inside the EL itself using an exist

相关标签:
3条回答
  • 2020-11-28 11:28

    Use <f:convertDateTime>. You can nest this in any input and output component. Pattern rules are same as java.text.SimpleDateFormat.

    <h:outputText value="#{someBean.dateField}" >
        <f:convertDateTime pattern="dd.MM.yyyy HH:mm" />
    </h:outputText>
    
    0 讨论(0)
  • 2020-11-28 11:30

    With EL 2 (Expression Language 2) you can use this type of construct for your question:

        #{formatBean.format(myBean.birthdate)}
    

    Or you can add an alternate getter in your bean resulting in

        #{myBean.birthdateString}
    

    where getBirthdateString returns the proper text representation. Remember to annotate the get method as @Transient if it is an Entity.

    0 讨论(0)
  • 2020-11-28 11:31

    If you use OmniFaces you can also use it's EL functions like of:formatDate() to format Date objects. You would use it like this:

    <h:outputText value="#{of:formatDate(someBean.dateField, 'dd.MM.yyyy HH:mm')}" />
    

    This way you can not only use it for output but also to pass it on to other JSF components.

    0 讨论(0)
提交回复
热议问题