All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.jarch.faces.converter.MonthYearJsfConverter 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.time.LocalDate;
import java.time.format.DateTimeFormatter;

import static java.time.format.DateTimeFormatter.ofPattern;

@FacesConverter("br.com.jarch.faces.converter.monthYearConverter")
public class MonthYearJsfConverter implements Serializable, Converter {
    @Override
    public LocalDate getAsObject(FacesContext context, UIComponent component, String value) {
        DateTimeFormatter formatter = ofPattern("dd/MM/yyyy");
        return LocalDate.parse("01/" + value, formatter);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return "";
        }

        LocalDate dateValue = (LocalDate) value;
        return dateValue.format(ofPattern("MM/yyyy"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy