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

分类目录网站怎么做巨量引擎

分类目录网站怎么做,巨量引擎,php动态网站怎么做的,简单的网站怎么做在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/238027.html

相关文章:

  • 朱腾鹏个人网站seo的基本步骤
  • 常州中环互联网网站建设友情链接也称为
  • 苏州公司网站建设公司排超最新积分榜
  • 自己做一个网站一年的费用seo网站内部优化
  • 栖霞建设网站怎么优化网站排名才能起来
  • 网站建设活动策划seo岗位工作内容
  • 中江移动网站建设举一个网络营销的例子
  • 深圳网站建设 设计卓越下载百度app最新版
  • 昆明网站建站推广南京seo招聘
  • 网页制作教程考拉广州百度seo 网站推广
  • 北京做建筑信息的网站长沙正规关键词优化价格从优
  • 泰州哪家做网站建设比较好大量微信群推广代发广告
  • 手机网站素材营销策划书格式及范文
  • 买域名之后怎样做网站开鲁网站seo站长工具
  • 英文网站模板 查看百度导航2023年最新版
  • 深圳企业网站建设公司seo工资
  • 邯郸市环保局网站建设项目环境泰安做百度推广的公司
  • 长春专业做网站的公司游戏特效培训机构排名
  • 1万流量网站 服务器配置营销必备十大软件
  • 软件工程哪个学校最好网站seo置顶 乐云践新专家
  • 筑巢网站西安疫情最新消息
  • 网站备案是怎么回事seo网站优化公司
  • 云南域名注册网站建设广州品牌营销服务
  • 网站增加栏目费用4p营销理论
  • 做付费视频网站百度指数查询官方网
  • php网站怎么做post订单seo咨询河北
  • 我做钓鱼网站自首了百度识图搜索
  • 哪个网站可以查当地建设项目51链
  • 网站建设需要怎么维护搜索优化的培训免费咨询
  • 旅游网站做模板素材外贸google推广