Spring MVC——Converter&ltString, Date&gt DEMO

問題描述

org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.util.Date] for value '1571846399000'; nested exception is java.lang.IllegalArgumentException

解決方案

方案一:

package org.javaboy.vhr.converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

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

/**
 * @作者 江南一點雨
 * @公衆號 江南一點雨
 * @微信號 a_java_boy
 * @GitHub https://github.com/lenve
 * @博客 http://wangsong.blog.csdn.net
 * @網站 http://www.javaboy.org
 * @時間 2019-11-20 8:08
 */
@Component
public class DateConverter implements Converter<String, Date> {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Date convert(String source) {
        try {
            return sdf.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

方案二:

package com.zstu.metrocity.converter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

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

/**
 * @Author ShenTuZhiGang
 * @Version 1.0.0
 * @Date 2020-05-23 17:55
 */
@Slf4j
@Component
public class DateConverter implements Converter<String, Date> {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private static final Pattern HH_MM_SS_SSS = Pattern.compile(".*[ ][0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{0,3}");

    private static final Pattern YYYY_MM_DD = Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}.*");

    private static final Pattern YYYY_M_D = Pattern.compile("^[0-9]{4}-[0-9]{1}-[0-9]+.*||^[0-9]{4}-[0-9]+-[0-9]{1}.*");

    private static final Pattern YY_MM_DD = Pattern.compile("^[0-9]{2}-[0-9]{2}-[0-9]{2}.*");

    private static final Pattern YY_M_D = Pattern.compile("^[0-9]{2}-[0-9]{1}-[0-9]+.*||^[0-9]{2}-[0-9]+-[0-9]{1}.*");

    private static final Pattern HH_MM_SS = Pattern.compile(".*[ ][0-9]{2}:[0-9]{2}:[0-9]{2}");

    private static final Pattern HH = Pattern.compile(".*[ ][0-9]{2}");

    private static final Pattern HH_MM = Pattern.compile(".*[ ][0-9]{2}:[0-9]{2}");
    

    /**
     * 字符串轉日期適配方法
     *
     * @param source 日期字符串
     * @throws
     */
    @Override
    public Date convert(String source) {

        Date date = null;
        if (!(null == source|| "".equals(source))) {
            //判斷是不是日期字符串,如Wed May 28 08:00:00 CST 2014
            if (source.contains("CST")) {
                date = new Date(source);
            } else {
                source = source.replace("年", "-").replace("月", "-").replace("日", "").replaceAll("/", "-").replaceAll("\\.", "-").trim();
                String fm = "";
                //確定日期格式
                if (YYYY_MM_DD.matcher(source).matches()) {
                    fm = "yyyy-MM-dd";
                } else if (YYYY_M_D.matcher(source).matches()) {
                    fm = "yyyy-M-d";
                } else if (YY_MM_DD.matcher(source).matches()) {
                    fm = "yy-MM-dd";
                } else if (YY_M_D.matcher(source).matches()) {
                    fm = "yy-M-d";
                }

                //確定時間格式
                if (HH.matcher(source).matches()) {
                    fm += "HH";
                } else if (HH_MM.matcher(source).matches()) {
                    fm += "HH:mm";
                } else if (HH_MM_SS.matcher(source).matches()) {
                    fm += "HH:mm:ss";
                } else if (HH_MM_SS_SSS.matcher(source).matches()) {
                    fm += "HH:mm:ss:sss";
                }
                if (!"".equals(fm)) {
                    try {
                        date = new SimpleDateFormat(fm).parse(source);
                    } catch (ParseException e) {
                        log.warn("參數字符串" + source + "無法被轉換爲日期格式!");
                    }
                }
            }
            if (date == null) {
                long l = Long.parseLong(source);
                date = new Date(l);
            }
        }
        return date;
    }

}

 

參考文章

https://blog.csdn.net/gwd1154978352/article/details/75041733

https://blog.csdn.net/dufuzhixinY/article/details/102756732

https://blog.csdn.net/sinat_30735061/article/details/96708826

https://blog.csdn.net/u011321758/article/details/80310678

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章