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

com.maccloud.spring.converter.DateConverter Maven / Gradle / Ivy

package com.maccloud.spring.converter;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.util.Date;

public class DateConverter implements Converter {

    public static final String[] parsePatterns = {
            "yyyy-MM-dd",
            "yyyy-MM-dd HH:mm:ss",
            "yyyy-MM",
            "yyyy",
            "yyyy-MM-dd HH",
            "yyyy-MM-dd HH:mm",
    };

    @Override
    public Date convert(String value) {
        return toDate(value);
    }

    public static Date toDate(String value) {
        try {
            return DateUtils.parseDate(value, parsePatterns);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy