This question already has an answer here:
-
How to use java.time.ZonedDateTime / LocalDateTime in p:calendar
1 answer
The JSF Core Tag f:convertDateTime can format java.util.Date objects. The Date class has many deprecated methods and with Java 8 come new classes to present local dates and times: LocalDateTime and LocalDate.
f:convertDateTime can not format LocalDateTime nor LocalDate.
Does anybody know, if there is an equivalent to the JSF core tag convertDateTime that can deal with LocalDateTime objects? Is support planned for a future release, or are alternative tags available?
Write your own Converter and extend the javax.faces.convert.DateTimeConverter
- that way you can Reuse all the attributes that <f:convertDateTime>
supports. Also it will take care of Localization too. Unfortunately its a bit more complicated to write a Converter with Attributes.
- Write your own Converter that extends
javax.faces.convert.DateTimeConverter
- just let the super-calls do all the work (including locale-stuff) and convert the result from/to LocalDate.
@FacesConverter(value = LocalDateConverter.ID)
public class LocalDateConverter extends DateTimeConverter {
public static final String ID = "com.example.LocalDateConverter";
@Override
public Object getAsObject(FacesContext facesContext,
UIComponent uiComponent, String value) {
LocalDate ldate = null;
Date date = null;
Object o = super.getAsObject(facesContext, uiComponent, value);
if(o == null) {
return null;
}
if (o instanceof Date) {
date = (Date) o;
Instant instant = Instant.ofEpochMilli(date.getTime());
ldate = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate();
return ldate;
}
else{
throw new IllegalArgumentException(String.format("value=%s could not be converted to a LocalDate, result super.getAsObject=%s",value,o));
}
}
@Override
public String getAsString(FacesContext facesContext,
UIComponent uiComponent, Object value) {
if(value == null){
return super.getAsString(facesContext, uiComponent,value);
}
if (value instanceof LocalDate) {
LocalDate lDate = (LocalDate) value;
Instant instant = lDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date date = Date.from(instant);
return super.getAsString(facesContext, uiComponent,date);
}
else{
throw new IllegalArgumentException(String.format("value=%s is not a instanceof LocalDate",value));
}
}
}
- Create a file
LocalDateConverter-taglib.xml
in META-INF
:
<facelet-taglib version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd">
<namespace>http://example.com/LocalDateConverter</namespace>
<tag>
<tag-name>convertLocalDate</tag-name>
<converter>
<converter-id>com.example.LocalDateConverter</converter-id>
</converter>
</tag>
</facelet-taglib>
- Register that taglib in
web.xml
:
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/META-INF/LocalDateConverter-taglib.xml</param-value>
</context-param>
- To use the new Tag in your JSF-Page add the new Taglib
xmlns:ldc="http://example.com/LocalDateConverter"
Then you can use the Tag <ldc:convertLocalDate type="both" dateStyle="full"/>
.