You can find the demonstrated code of this post on my Github repo here.
The user story
" As a banker, I want to enter a client's birthday like 01.01.80 or 01.01.1980,So that the birthday can be displayed as 01.01.1980 "
Implementation
Firstly, I thought about how to use a built-in converter likes the following.<h:inputText id="birthdate"
value="#{data.birthdate}"
type="date" >
<f:convertDateTime/>
<f:ajax event="change"
listener="#{data.onCalculate}"
execute="@this"
render="@this" />
</h:inputText>
However, without defining a pattern, JSF used its default one which was not my desire. It threw an exception when I tried to enter a date like "01.01.90".
> myform:birthdate: '01.01.90' could not be understood as a date. Example: Mar 4, 2018
Actually, I even could not define either pattern "dd.MM.yyyy" or "dd.MM.yy" for "f:convertDateTime":
- "dd.MM.yyyy" --> 01.01.80 becomes 01.01.0080
- "dd.MM.yy" --> 01.01.1980 becomes 01.01.80
Well, this was a case for us to define our own custom converter.
- XHTML code
<h:inputText id="birthdate"
value="#{data.birthdate}"
type="date">
<f:converter converterId="myDateTimeConverter" />
<f:ajax event="change"
listener="#{data.onCalculate}"
execute="@this"
render="@this" />
</h:inputText>
- Java code
package vn.nvanhuong.javalab.jsf.dateformat;The idea was that I allowed users to enter the year with 4 or 2 digits. Then, I used SimpleDateFormatter to convert the entered value into a value with type Date at JSF's phase "Apply Request" which calls method "getAsObject".
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import org.apache.commons.lang3.StringUtils;
@FacesConverter("myDateTimeConverter")
public class DateTimeConverter implements Converter {
private static final String DD_MM_YY = "dd.MM.yy";
private static final String DD_MM_YYYY = "dd.MM.yyyy";
public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
if (StringUtils.isEmpty(value)) {
return null;
}
SimpleDateFormat formatter = new SimpleDateFormat(DD_MM_YY);
Date parsedObject = null;
try {
parsedObject = formatter.parse(value);
} catch (ParseException e) {
throw new ConverterException(e);
}
return parsedObject;
}
public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
if (value == null) {
return null;
}
SimpleDateFormat formatter = new SimpleDateFormat(DD_MM_YYYY);
return formatter.format(value);
}
}
The cool thing here was with pattern "dd.MM.yy" the formatter allowed us to do that! Then, we always displayed the date with format "dd.MM.yyyy" at JSF's phase "Render Response" which calls method "getAsString".