package com.fp.frontend.converters;

import java.text.SimpleDateFormat;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 * Clase que se encarga de convertir un String a java.sql.Date y viceversa.
 *
 * @author Jorge Vaca.
 * @version 2.1
 */
@FacesConverter("converter.date")
public class DateConverter implements Converter {

    private static SimpleDateFormat FORMATOFECHA = new SimpleDateFormat("dd-MM-yyyy");

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        java.sql.Date sqlDate = null;
        try {
            java.util.Date dt = FORMATOFECHA.parse(value);
            sqlDate = new java.sql.Date(dt.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sqlDate;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return FORMATOFECHA.format(value);
    }

}
