后台将前端传过来的时间字符串转化为date类型并以yyyy

您所在的位置:网站首页 无法将字符串转换为date 后台将前端传过来的时间字符串转化为date类型并以yyyy

后台将前端传过来的时间字符串转化为date类型并以yyyy

2024-07-13 15:48| 来源: 网络整理| 查看: 265

前端传过来的时间类型一般为字符串类型,而我们的数据库使用字段类型一般为datetime或者date类型时,后台就需要将字符串类型的时间转化为datetime或者date类型才可以保存进数据库。同时,如果前端传过来时间格式如:yyyy、yyyy-MM、yyyy-MM-dd、yyyy-MM-dd HH、yyyy-MM-dd HH:mm、yyyy-MM-dd HH:mm:ss等等多种格式的话,我们就需要将时间格式化才能够成功村进数据库了。 1、创建工具类将string类型时间转为为date类型:

package com.ice.config; import org.springframework.core.convert.converter.Converter; import org.springframework.lang.Nullable; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * @author 紫风 * @date 2021年09月12日 16:22 */ public class StringToDateConverter implements Converter { @Nullable @Override public Date convert(String json) { System.out.println(json.length()); System.out.println(json); if (json.length() == 10) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { return simpleDateFormat.parse(json); } catch (ParseException e) { e.printStackTrace(); } return null; } if (json.length() == 13) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH"); try { return simpleDateFormat.parse(json); } catch (ParseException e) { e.printStackTrace(); } return null; } if (json.length() == 16) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); try { return simpleDateFormat.parse(json); } catch (ParseException e) { e.printStackTrace(); } return null; } if (json.length() == 19) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return simpleDateFormat.parse(json); } catch (ParseException e) { e.printStackTrace(); } return null; } return null; } }

2、添加时间数据处理的配置类,实现WebMvcConfigurer类

package com.ice.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.support.GenericConversionService; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.List; /** * @author 紫风 * @date 2021年09月12日 16:24 */ //时间数据的处理 @Configuration public class MySpringMvcConfigurer implements WebMvcConfigurer { @Autowired private RequestMappingHandlerAdapter requestMappingHandlerAdapter; @PostConstruct public void addConversionConfig() { ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) requestMappingHandlerAdapter.getWebBindingInitializer(); if (initializer.getConversionService() != null) { GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService(); //添加字符串转换为日期类型的字符串 genericConversionService.addConverter(new StringToDateConverter()); } }

3、如果前端需要接收各种各样格式的时间数据,后台可以在实体类时间对象上添加如下注解

@JsonFormat(pattern = "yyyy年MM月dd日", timezone = "GMT+8") @JsonFormat(pattern = "yyyy年MM月dd日HH时", timezone = "GMT+8") @JsonFormat(pattern = "yyyy年MM月dd日HH时mm分",timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd HH", timezone = "GMT+8") @JsonFormat(pattern = "yyyy-MM-dd HH:mm",timezone = "GMT+8")

等等



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3