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

校园网站设计开题报告中山网站定制公司

校园网站设计开题报告,中山网站定制公司,wordpress用户前端创建相册,微信视频网站怎么做的时间格式化在项目中使用频率是非常高的#xff0c;当我们的 API 接口返回结果#xff0c;需要对其中某一个 date 字段属性进行特殊的格式化处理#xff0c;通常会用到 SimpleDateFormat 工具处理。SimpleDateFormat dateFormat new SimpleDateFormat(yyyy-MM-dd当我们的 API 接口返回结果需要对其中某一个 date 字段属性进行特殊的格式化处理通常会用到 SimpleDateFormat 工具处理。SimpleDateFormat dateFormat  new SimpleDateFormat(yyyy-MM-dd); Date stationTime  dateFormat.parse(dateFormat.format(PayEndTime())); 可一旦处理的地方较多不仅 CV 操作频繁还产生很多重复臃肿的代码而此时如果能将时间格式统一配置就可以省下更多时间专注于业务开发了。可能很多人觉得统一格式化时间很简单啊像下边这样配置一下就行了但事实上这种方式只对 date 类型生效。spring.jackson.date-formatyyyy-MM-dd HH:mm:ss spring.jackson.time-zoneGMT8 而很多项目中用到的时间和日期API 比较混乱 java.util.Date 、 java.util.Calendar 和 java.time LocalDateTime 都存在所以全局时间格式化必须要同时兼容性新旧 API。看看配置全局时间格式化前接口返回时间字段的格式。Data public class OrderDTO {private LocalDateTime createTime;private Date updateTime; }很明显不符合页面上的显示要求有人抬杠为啥不让前端解析时间我只能说睡服代码比说服人容易得多~未做任何配置的结果一、JsonFormat 注解JsonFormat 注解方式严格意义上不能叫全局时间格式化应该叫部分格式化因为JsonFormat 注解需要用在实体类的时间字段上而只有使用相应的实体类对应的字段才能进行格式化。Data public class OrderDTO {JsonFormat(locale  zh, timezone  GMT8, pattern  yyyy-MM-dd)private LocalDateTime createTime;JsonFormat(locale  zh, timezone  GMT8, pattern  yyyy-MM-dd HH:mm:ss)private Date updateTime; } 字段加上 JsonFormat 注解后LocalDateTime 和 Date 时间格式化成功。JsonFormat 注解格式化二、JsonComponent 注解推荐这是我个人比较推荐的一种方式前边看到使用 JsonFormat 注解并不能完全做到全局时间格式化所以接下来我们使用 JsonComponent 注解自定义一个全局格式化类分别对 Date 和 LocalDate 类型做格式化处理。 JsonComponent public class DateFormatConfig {Value(${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss})private String pattern;/*** author xiaofu* description date 类型全局时间格式化* date 2020/8/31 18:22*/Beanpublic Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() {return builder - {TimeZone tz  TimeZone.getTimeZone(UTC);DateFormat df  new SimpleDateFormat(pattern);df.setTimeZone(tz);builder.failOnEmptyBeans(false).failOnUnknownProperties(false).featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).dateFormat(df);};}/*** author xiaofu* description LocalDate 类型全局时间格式化* date 2020/8/31 18:22*/Beanpublic LocalDateTimeSerializer localDateTimeDeserializer() {return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));}Beanpublic Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {return builder - builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());} } 看到 Date 和 LocalDate 两种时间类型格式化成功此种方式有效。JsonComponent 注解处理格式化但还有个问题实际开发中如果我有个字段不想用全局格式化设置的时间样式想自定义格式怎么办那就需要和 JsonFormat 注解配合使用了。Data public class OrderDTO {JsonFormat(locale  zh, timezone  GMT8, pattern  yyyy-MM-dd)private LocalDateTime createTime;JsonFormat(locale  zh, timezone  GMT8, pattern  yyyy-MM-dd)private Date updateTime; } 从结果上我们看到 JsonFormat 注解的优先级比较高会以 JsonFormat 注解的时间格式为主。三、Configuration 注解这种全局配置的实现方式与上边的效果是一样的。“注意在使用此种配置后字段手动配置JsonFormat 注解将不再生效。” Configuration public class DateFormatConfig2 {Value(${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss})private String pattern;public static DateFormat dateFormat  new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);BeanPrimarypublic ObjectMapper serializingObjectMapper() {ObjectMapper objectMapper  new ObjectMapper();JavaTimeModule javaTimeModule  new JavaTimeModule();javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());objectMapper.registerModule(javaTimeModule);return objectMapper;}/*** author xiaofu* description Date 时间类型装换* date 2020/9/1 17:25*/Componentpublic class DateSerializer extends JsonSerializerDate {Overridepublic void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException {String formattedDate  dateFormat.format(date);gen.writeString(formattedDate);}}/*** author xiaofu* description Date 时间类型装换* date 2020/9/1 17:25*/Componentpublic class DateDeserializer extends JsonDeserializerDate {Overridepublic Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {try {return dateFormat.parse(jsonParser.getValueAsString());} catch (ParseException e) {throw new RuntimeException(Could not parse date, e);}}}/*** author xiaofu* description LocalDate 时间类型装换* date 2020/9/1 17:25*/public class LocalDateTimeSerializer extends JsonSerializerLocalDateTime {Overridepublic void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern)));}}/*** author xiaofu* description LocalDate 时间类型装换* date 2020/9/1 17:25*/public class LocalDateTimeDeserializer extends JsonDeserializerLocalDateTime {Overridepublic LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException {return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(pattern));}} } 总结分享了一个简单却又很实用的 Springboot 开发技巧其实所谓的开发效率不过是一个又一个开发技巧堆砌而来聪明的程序员总是能用最少的代码完成任务。 往期推荐 定时任务最简单的3种实现方法超好用2020-08-18 List 集合去重的 3 种方法2020-08-17 4种分布式Session的实现方式老大直呼666...2020-07-23
http://www.sadfv.cn/news/196224/

相关文章:

  • 企业网站建设方案书目录装修招投标网站建设
  • ps做网站首页网站建设方案书1500字
  • 中文网站建设哪家好个人简历模板网站
  • 公司网站优化去哪里学腾讯会议开始收费
  • 建设汽车网站用幽默的语言来形容网站开发
  • 浙江建设培训中心网站目前做哪些网站致富
  • 广州企业网站seo临沂做网站公司哪家好
  • 网站怎么做关键词优化百度登录注册
  • 适合学生做的网站类型网络营销就是网络销售
  • 网站发的文章如何优化世界青田网app
  • 在线视频网站如何制作建设一个下载资料的网站
  • 软件工程毕业可以做网站吗网站模块划分规划
  • 网站建设案例分享门户建设开源软件
  • 网站建设会议报道如何改wordpress主页
  • 免费网站制作软件平台做网站用vps还是虚拟主机
  • 桥头镇网站仿做大连建设网网址
  • 做外贸有效的网站建筑人才网官方网站入口
  • 浏览器怎么打开网站服务器我想自学建网站
  • 北京营销推广网站建设哈尔滨免费做网站
  • 徐州网站设计制作建设揭阳网站开发mituad
  • 网站建设人员培训纲要建立的短语
  • 响应式网站是啥意思盐城网络优化
  • 政务类网站wordpress 主题 轮播
  • 使用网站的mysql重庆网站建设技术托管
  • 一个人网站运营怎么做门户系统登录
  • 岳阳网站建设设计网站建设管理员工工资多少
  • 微网站的链接怎么做哪种语言做网站最快
  • 建设 公司 网站 请示阿里云网站怎么备案域名解析
  • 营销型网站设计服务商怎样选wordpress主题
  • 北京网站网页设计手机优化助手下载