
br.com.jarch.faces.converter.LocalDateJsfConverter Maven / Gradle / Ivy
package br.com.jarch.faces.converter;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import static java.time.format.DateTimeFormatter.ofPattern;
@FacesConverter("br.com.jarch.faces.converter.localDateConverter")
public class LocalDateJsfConverter implements Serializable, Converter {
public static final int SEVEN = 7;
@Override
public LocalDate getAsObject(FacesContext context, UIComponent component, String value) {
String newValue = value;
if (newValue.length() == SEVEN) {
newValue = "01/" + newValue;
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
return LocalDate.parse(newValue, formatter);
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null) {
return "";
}
if (Date.class.isAssignableFrom(value.getClass())) {
Date date = (Date) value;
return new SimpleDateFormat("dd/MM/yyyy").format(date);
}
LocalDate dateValue = (LocalDate) value;
return dateValue.format(ofPattern("dd/MM/yyyy"));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy