javascript
Spring MVC——ConverterltString, Dategt DEMO
問(wèn)題描述
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;/*** @作者 江南一點(diǎn)雨* @公眾號(hào) 江南一點(diǎn)雨* @微信號(hào) a_java_boy* @GitHub https://github.com/lenve* @博客 http://wangsong.blog.csdn.net* @網(wǎng)站 http://www.javaboy.org* @時(shí)間 2019-11-20 8:08*/ @Component public class DateConverter implements Converter<String, Date> {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");@Overridepublic 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}");/*** 字符串轉(zhuǎn)日期適配方法** @param source 日期字符串* @throws*/@Overridepublic Date convert(String source) {Date date = null;if (!(null == source|| "".equals(source))) {//判斷是不是日期字符串,如Wed May 28 08:00:00 CST 2014if (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";}//確定時(shí)間格式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("參數(shù)字符串" + source + "無(wú)法被轉(zhuǎn)換為日期格式!");}}}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
總結(jié)
以上是生活随笔為你收集整理的Spring MVC——ConverterltString, Dategt DEMO的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C#——《C#语言程序设计》实验报告——
- 下一篇: C++——《数据结构与算法》实验——排序