package me.chyxion.tigon.webmvc.converter;

import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Oct 19, 2015 7:45:36 PM
 */
public class StringToDateTypeConverter 
    implements Converter<String, Date> {

    /**
     * {@inheritDoc}
     */
    @Override
    public Date convert(String text) {
        try {
            return StringUtils.isNotBlank(text) ? 
                new Date(Long.parseLong(text.trim())) : null;
        }
        catch (NumberFormatException e) {
            throw new IllegalArgumentException(
                "Invalid Date [" + text + "] Param", e);
        }
    }
}
