I have a local date time property:
private LocalDatetime date;
I want to show it in this format dd-MM-yyyy HH:mm:ss:
<h:outputText value="#{transaction.date}">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm:ss"/>
</h:outputText>
But I got this exception:
java.lang.IllegalArgumentException: Cannot format given Object as a Date
How is this caused and how can I solve it?
The
<f:convertDateTime>defaults currently still tojava.util.Dateas expected date type and is currently not (yet?) capable of automatically detecting the date type. It will under the covers incorrectly continue to usejava.text.SimpleDateFormatinstead ofjava.time.format.DateTimeFormatterto format theLocalDateTime. This specific exception is coming fromSimpleDateFormat.You need to explicitly set the
typeattribute of the<f:convertDateTime>tag to the desired date type if your actual date value is not ajava.util.Date. You can find a list of supported types in in tag documentation:typefalsejavax.el.ValueExpression(must evaluate to
java.lang.String)java.timewith the name derived by upper casing the first letter. For example,java.time.LocalDatefor the value "localDate". Default value is "date".So, in your specific case, setting it to
localDateTimeshould fix it:In case it is output-only and you happen to use the utility library OmniFaces of version 3.6 or newer, then you can also use the
#{of:formatDate()}function instead. It is capable of automatically detecting the date type and it currently supports all known instances ofjava.util.Date,java.util.Calendarandjava.time.Temporal.