当前位置: 首页 > wzjs >正文

网站开发实训感想广告免费发布信息平台

网站开发实训感想,广告免费发布信息平台,校园网站建设培训体会,网站分页设计作用在Spring MVC中,自定义日期类型格式转换器可以通过实现Converter接口或使用DateTimeFormat注解。以下是两种方法的详细说明: 方法一:全局自定义转换器(推荐) 1. 创建日期转换器类 实现 org.springframework.core.con…

在Spring MVC中,自定义日期类型格式转换器可以通过实现Converter接口或使用@DateTimeFormat注解。以下是两种方法的详细说明:


方法一:全局自定义转换器(推荐)

1. 创建日期转换器类

实现 org.springframework.core.convert.converter.Converter 接口:

import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class StringToDateConverter implements Converter<String, Date> {private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";@Overridepublic Date convert(String source) {SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);try {return format.parse(source);} catch (ParseException e) {throw new IllegalArgumentException("日期格式错误,请使用 " + DATE_FORMAT, e);}}
}
2. 注册转换器到Spring容器
方法1:通过配置类实现 WebMvcConfigurer
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addFormatters(FormatterRegistry registry) {registry.addConverter(new StringToDateConverter());}
}
方法2:通过配置applicationContex.xml实现:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 1. 声明自定义转换器 --><bean id="stringToDateConverter" class="com.example.converter.StringToDateConverter"/><!-- 2. 配置 ConversionService --><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><set><ref bean="stringToDateConverter"/><!-- 可添加其他转换器,如日期转字符串 --></set></property></bean><!-- 3. 启用 MVC 注解驱动并关联 ConversionService --><mvc:annotation-driven conversion-service="conversionService"/><!-- 其他配置(如组件扫描、视图解析器等) --><context:component-scan base-package="com.example.controller"/>
</beans>

方法二:局部使用 @DateTimeFormat

直接在实体类字段上标注日期格式:

import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;public class MyEntity {@DateTimeFormat(pattern = "yyyy/MM/dd")private Date createTime;// Getter & Setter
}

使用 Java 8 的 DateTimeFormatter(更安全)

如果使用Java 8+,建议用 DateTimeFormatter 替代 SimpleDateFormat

import org.springframework.core.convert.converter.Converter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;public class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");@Overridepublic LocalDateTime convert(String source) {return LocalDateTime.parse(source, FORMATTER);}
}

注册方式与全局转换器相同。


注意事项:

  1. 时区处理:如果涉及跨时区,需在格式化时指定时区:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
    
  2. 异常处理:转换失败时会抛出 IllegalArgumentException,可在Controller中捕获并处理。

  3. 多格式支持:若需支持多种格式,可在转换器中按顺序尝试不同格式。


通过以上步骤,即可实现全局或局部的日期格式自定义转换。

http://www.dtcms.com/wzjs/561351.html

相关文章:

  • 网站开发如何查看请求体黄山建设工程信息网站
  • 织梦搞笑图片网站源码外贸运营推广方式
  • 北京知名网站加工平台制作
  • 如何建网站开网店网站建设推广工资
  • 国内网站开发的主流技术建设银行龙卡信用卡官方网站
  • 响应式网站管理系统广州网站优化流程
  • 医学关键词 是哪个网站做东莞人社小程序
  • 中壹建设工程有限公司官方网站网站后台有安全狗
  • 房屋装修设计网站电子商务网站功能模块
  • 网站的营销策略零基础做网站教程
  • 会展网站建设成功的原因互动网站制作
  • 天台县网站建设哪家好铜仁市网站建设
  • wordpress建站好么军事新闻国际军事新闻
  • 网站的百度推广怎么做wordpress 删除模板
  • 房子信息查询网站入口雅安城乡住房建设厅网站
  • 网站建设售后服务安全维护wordpress 二次开发视频教程下载
  • 浅谈电子商务网站建设与管理的理解vscode创建网页
  • 建设网站费用多少柳州建设网官方网站
  • 自已做的网站怎么做域名解析郑州做网站优化外包
  • 福永外贸网站建设公司温岭 网站制作
  • 网站用什么系统好用wordpress网页视频播放器
  • 沈阳做网站在哪直播软件app下载免费
  • 最简单的网站系统青岛知名网站建设
  • 模版网站建设企业知名手机网站
  • 一个虚拟主机可以做几个网站表白网址一键生成
  • 淘宝网站建设退款雷州网站开发公司
  • 哪些网站做京东的团购邮箱网址注册免费
  • 网站开发的项目实战微博营销网站
  • 重庆百度网站快速排名政务公开加强网站规范化建设
  • 学校网站注重服务平台建设信通网站开发中心