Spring mvc日期转换为字符串

package com.hgd.ssm.utils;

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

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

/**
 * @auther 黄国栋
 * @data 2020-06-29 14:39
 * @since
 */
public class DateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String s) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try{
            return simpleDateFormat.parse(s);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">

        <!--配置处理器适配器和处理器映射器-->
        <mvc:annotation-driven conversion-service="conversionService"/>

        <!--配置转换器-->
        <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
            <property name="converters">
                <set>
                    <bean class="com.hgd.ssm.utils.DateConverter"></bean>
                </set>
            </property>
        </bean>

        <!--配置视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>

        <!--扫描controller-->
        <context:component-scan base-package="com.hgd.ssm.controller"/>

        <!--文件上传-->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!--上传文件的大小-->
            <property name="maxUploadSize" value="100000000"></property>
        </bean>

</beans>
<hr>
    <a href="user/deleteUser?birthday=2018-01-01">根据日期删除用户(String)</a>
    <hr>
    <a href="user/deleteUser2?birthday=2018-01-01 11:12:00">根据日期删除用户(Data)</a>
//根据日期删除用户(String)
@RequestMapping("deleteUser")
public String deleteUser(String birthday){
  System.out.println("birthday = " + birthday);
  return "success";
}

//根据日期删除用户(Data)
@RequestMapping("deleteUser2")
public String deleteUser2(Date birthday){
  System.out.println("birthday = " + birthday);
  return "success";
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值