SpringMVC 日期类型转换问题的处理方法

背景


写程序的时候遇到了 前端传入的数据类型为 字符串的日期数据,后端接受的应该是 Date类型的日期数据 而报错的问题。

Field error in object 'studentCustom' on field 'birthyear': rejected value [1979-01-01]; codes [typeMismatch.studentCustom.birthyear,typeMismatch.birthyear,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [studentCustom.birthyear,birthyear]; arguments []; default message [birthyear]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'birthyear'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '1979-01-01'; nested exception is java.lang.IllegalArgumentException]

SpringMVC日期类型转换问题三大处理方法归纳

ssm整合快速入门程序(三)之Data类型转换器


方法一:实体类中加日期格式化注解


@DateTimeFormat(pattern = "yyyy-MM-dd")  
private Date receiveAppTime;  

如上,在对应的属性上,加上指定日期格式的注解


方法二:实现一个全局日期类型转换器并进行配置


springmvc配置Data类型转换器

在创建一个转换器的包cn.my.ssm.controller.converter,创建一个CustomDateConverter类实现Converter

package cn.my.ssm.controller.converter;

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

import org.springframework.core.convert.converter.Converter;
/**
 * date类型转换
 *
 */
public class CustomDateConverter implements Converter<String, Date>  {

    @Override
    public Date convert(String source) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return simpleDateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}

springmvc.xml

 <!--   配置转换器(比如Date类型) -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="cn.my.ssm.controller.converter.CustomDateConverter" />
        </list>
    </property>
</bean>

jsp 页面配置

<!--使用jstl标签返回时间格式-->
<p><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></p>

方法三:适合页面把日期类型转换成字符串且JSP,Freemark页面


JSP模版引擎方法:

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> 
<fmt:formatDate value="${job.jobtime }" pattern="yyyy-MM-dd HH:mm:ss"/>

Freemarker模版引擎方法:

<input id="receiveAppTime" name="receiveAppTime" type="text" value="${(bean.receiveAppTime?string('yyyy-MM-dd'))!}" />  

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMVC提供了三种类型转换的接口,分别是Formatter、Converter和原生类型转换器。 1. Formatter接口:Formatter接口用于将字符串转换为目标类型,并将目标类型转换为字符串。它可以用于处理日期、数字等常见的数据类型转换。Formatter接口定义了两个方法:`parse()`用于将字符串转换为目标类型,`print()`用于将目标类型转换为字符串。 2. Converter接口:Converter接口用于将一种数据类型转换为另一种数据类型。它定义了两个方法:`convert()`用于将源类型转换为目标类型,`reverse()`用于将目标类型转换回源类型。开发者可以根据自己的需求实现Converter接口来实现特定功能的类型转换。 3. 原生类型转换器:SpringMVC内置了许多原生类型转换器,可以将常见的数据类型进行转换,例如将字符串转换为整型、将字符串转换为日期等。开发者在实际应用中使用框架内置的类型转换器基本上就够了,但有时需要编写具有特定功能的类型转换器。 下面是一个使用Formatter接口的示例: ```java import org.springframework.format.Formatter; public class MyDateFormatter implements Formatter<Date> { @Override public Date parse(String text, Locale locale) throws ParseException { // 将字符串转换为日期类型 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); return dateFormat.parse(text); } @Override public String print(Date date, Locale locale) { // 将日期类型转换为字符串 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); return dateFormat.format(date); } } ``` 下面是一个使用Converter接口的示例: ```java import org.springframework.core.convert.converter.Converter; public class MyStringToIntegerConverter implements Converter<String, Integer> { @Override public Integer convert(String source) { // 将字符串转换为整型 return Integer.parseInt(source); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值