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

message.mvc.convert.StringDateConvertFactory Maven / Gradle / Ivy

There is a newer version: 2.5.9
Show newest version
package message.mvc.convert;

import message.base.utils.StringUtils;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * 时间类型转换器.
 *
 * @author sunhao([email protected])
 * @version V1.0
 * @createTime 2014-12-18 15:28
 */
public class StringDateConvertFactory implements ConverterFactory, ConditionalConverter {
    @Override
    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
        Class clazz = targetType.getType();
        return Date.class.isAssignableFrom(clazz);
    }

    @Override
    public  Converter getConverter(Class targetType) {
        return new String2Date();
    }

    private class String2Date implements Converter {
        @Override
        public Date convert(String source) {
            try {
                String pattern = getDateFormatPattern(source);
                DateFormat df = new SimpleDateFormat(pattern);

                return df.parse(source);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * get date format pattern for what parameter in request
         *
         * @param source
         * @return
         */
        private String getDateFormatPattern(String source) {
            if (StringUtils.isEmpty(source)) {
                return "yyyy-MM-dd HH:mm";
            }
            //yyyy-MM-dd hh:mm
            Pattern p1 = Pattern.compile("(\\d){2,4}[-](\\d){1,2}[-](\\d){1,2} (\\d){1,2}[:](\\d){1,2}");
            //yyyy-MM-dd
            Pattern p2 = Pattern.compile("(\\d){2,4}[-](\\d){1,2}[-](\\d){1,2}");
            //hh:mm yyyy-MM-dd
            Pattern p3 = Pattern.compile("(\\d){1,2}[:](\\d){1,2} (\\d){2,4}[-](\\d){1,2}[-](\\d){1,2}");

            if (p1.matcher(source).matches()) {
                return "yyyy-MM-dd HH:mm";
            }
            if (p2.matcher(source).matches()) {
                return "yyyy-MM-dd";
            }
            if (p3.matcher(source).matches()) {
                return "HH:mm yyyy-MM-dd";
            }

            return "yyyy-MM-dd HH:mm";
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy