Spring时间(Date)类型转换+自定义

第一种方法:利用内置的 CustomDateEditor(不推荐,每个类都需要定义)

首先,在我们的 Controller 的 InitBinder 里面,注册 CustomEditor

//首先初始化这个方法
    @InitBinder
    public void init (WebDataBinder binder) {
        CustomDateEditor dateEditor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true);
        binder.registerCustomEditor(Date.class, dateEditor);
    }

当提交的时候,会进入

 

Spring配置Converter、Formatter日期转换

第二种方法:实现自定义 Converter或者Formatter<Date>

首先创建一个类继承Converter<String, Date>接口

package com.wbg.maven1128.miscellanueous;


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

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

public class MuDateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String value) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date=null;
        try {
             date = simpleDateFormat.parse(value);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}
View Code

或者一个类继承Formatter<Date>

package com.wbg.maven1128.miscellanueous;

import org.springframework.format.Formatter;

import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class MDateFormatter implements Formatter<Date> {

    // 日期类型模板:如yyyy-MM-dd   默认
    private String datePattern="yyyy-MM-dd";
    // 日期格式化对象
    private SimpleDateFormat dateFormat;
    // 构造器,通过依赖注入的日期类型创建日期格式化对象
    public MDateFormatter(String datePattern){
        super();
        this.datePattern=datePattern;
        dateFormat = new SimpleDateFormat(datePattern);
    }
    @Override
    public String print(Date data, Locale locale) {
        dateFormat.setLenient(false);
        return dateFormat.format(data);
    }
    // 解析文本字符串返回一个Formatter<T>的T类型对象
    @Override
    public Date parse(String text, Locale locale) throws ParseException {
        return dateFormat.parse(text);
    }
}
View Code

接口

 

然后xml配置并注册 ConversionService:

converters

<bean name="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.wbg.maven1128.miscellanueous.MuDateConverter" />
            </set>
        </property>
    </bean>
    <!--启用-->
    <mvc:annotation-driven conversion-service="conversionService" />
View Code

 factoryBean

<bean name="factoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="formatters">
            <set>
                <bean class="com.wbg.maven1128.miscellanueous.MDateFormatter" >
                    <constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
                </bean>
            </set>
        </property>
    </bean>
    <mvc:annotation-driven conversion-service="factoryBean"/>
View Code

提交的时候

 

第三种方法:使用 @DateTimeFormat 注解

 https://www.cnblogs.com/weibanggang/p/9903024.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC中,您可以使用自定义类型转换器来处理特定类型之间的转换。要设置自定义类型转换器,您需要执行以下步骤: 1. 创建一个实现了 `Converter<S, T>` 接口的自定义类型转换器类。其中,S 是源类型,T 是目标类型。该接口要求实现 `convert()` 方法,用于执行实际的类型转换逻辑。 ```java public class CustomConverter implements Converter<String, Date> { @Override public Date convert(String source) { // 实现自定义类型转换逻辑 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { return dateFormat.parse(source); } catch (ParseException e) { throw new IllegalArgumentException("Invalid date format"); } } } ``` 2. 在Spring MVC配置中注册自定义类型转换器。可以通过编程方式或者使用XML配置进行注册。 a. 编程方式: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new CustomConverter()); } } ``` b. XML配置方式: ```xml <mvc:annotation-driven conversion-service="conversionService" /> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.example.CustomConverter" /> </set> </property> </bean> ``` 3. 在您的控制器方法中使用目标类型作为参数。Spring MVC会自动调用适当的类型转换器将请求参数转换为目标类型。 ```java @Controller public class MyController { @RequestMapping("/example") public String handleRequest(@RequestParam("date") Date date) { // 处理转换后的日期对象 return "example"; } } ``` 通过以上步骤,您就可以设置自定义类型转换器来处理Spring MVC中的类型转换。希望对您有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值