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

制作一个论坛网站多少钱做宣传图册在什么网站

制作一个论坛网站多少钱,做宣传图册在什么网站,网站的内链,wordpress刷留言本文转载自#xff1a;springboot#xff1a;时间格式化的5种方法#xff08;解决后端传给前端的时间显示不一致#xff09;_为什么前端格式化日期了后端还要格式化_洛泞的博客-CSDN博客 时间问题演示 为了方便演示#xff0c;我写了一个简单 Spring Boot 项目#xff…本文转载自springboot时间格式化的5种方法解决后端传给前端的时间显示不一致_为什么前端格式化日期了后端还要格式化_洛泞的博客-CSDN博客 时间问题演示 为了方便演示我写了一个简单 Spring Boot 项目其中数据库中包含了一张 userinfo 表它 的组成结构和数据信息如下 项目目录是这样的 UserController 实现代码如下  RestController RequestMapping(/user) publicclass UserController {Resourceprivate UserMapper userMapper;RequestMapping(/list)public ListUserInfo getList() {return userMapper.getList();} }UserMapper 实现代码如下 Mapper public interface UserMapper {public ListUserInfo getList(); }UserInfo 实现代码如下 Data publicclass UserInfo {privateint id;private String username;private Date createtime;private Date updatetime; }UserMapper.xml 实现代码如下 ?xml version1.0 encodingUTF-8? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.example.demo.mapper.UserMapperselect idgetList resultTypecom.example.demo.model.UserInfoselect * from userinfo/select /mapper经过以上内容的编写我们就制作出了一个简单的 Spring Boot 项目了。接下来我们使用 PostMan 来模拟调用 UserController 接口执行结果如下 从上述结果可以看出时间字段 createtime 和 updatetime 的显示方式是很“凌乱”的并不符合我们的阅读习惯也不能直接展示给前端的用户使用这时候我们就需要对时间进行格式化处理了。 时间格式化的方法总共包含以下 5 种。 1.前端时间格式化 JS 版时间格式化 假设您有一个名为data的数组其中包含后端响应的数据其中包含了createdTime字段 createdTime: { nano: 0, year: 2021, monthValue: 12, dayOfMonth: 16, hour: 5, minute: 54, second: 45, dayOfWeek: THURSDAY, dayOfYear: 350, month: DECEMBER, chronology: { id: ISO, calendarType: iso8601 } } 您可以像这样在el-table中显示它  vue templatedivel-table :datadatael-table-column label创建时间template slot-scopescope{{ formatDate(scope.row.createdTime) }}/template/el-table-column!-- 其他列 --/el-table/div /templatescript export default {data() {return {data: [], // 后端响应的数据};},methods: {formatDate(timestamp) {// 使用JavaScript的Date对象将时间戳格式化为某年-某月-某日 某时:某分:某秒const date new Date(timestamp.year, timestamp.monthValue - 1, timestamp.dayOfMonth, timestamp.hour, timestamp.minute, timestamp.second);const year date.getFullYear();const month (date.getMonth() 1).toString().padStart(2, 0); // 补零const day date.getDate().toString().padStart(2, 0);const hours date.getHours().toString().padStart(2, 0);const minutes date.getMinutes().toString().padStart(2, 0);const seconds date.getSeconds().toString().padStart(2, 0);return ${year}-${month}-${day} ${hours}:${minutes}:${seconds};},}, }; /script 2.SimpleDateFormat格式化(Date) 使用 SimpleDateFormat 来进行时间格式化它也是 JDK 8 之前重要的时间格式化方法它的核心实现代码如下 // 定义时间格式化对象和定义格式化样式 SimpleDateFormat dateFormat new SimpleDateFormat(yyyy-MM-dd HH:mm:ss); // 格式化时间对象 String date dateFormat.format(new Date())接下来我们使用 SimpleDateFormat 来实现一下本项目中的时间格式化它的实现代码如下 RequestMapping(/list) public ListUserInfo getList() {// 定义时间格式化对象SimpleDateFormat dateFormat new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);ListUserInfo list userMapper.getList();// 循环执行时间格式化list.forEach(item - {// 使用预留字段 ctime 接收 createtime 格式化的时间(Date-String)item.setCtime(dateFormat.format(item.getCreatetime()));item.setUtime(dateFormat.format(item.getUpdatetime()));});return list; }程序执行结果如下 从上述结果可以看出时间格式化没有任何问题以及到底我们预想的目的了。但细心的读者会发现为什么接口的返回字段咋变了呢之前的字段是 createtime 现在却是 ctime… 这是因为使用 #SimpleDateFormat.format 方法之后它返回的是一个 String 类型的结果而我们之前的 createtime 和 updatetime 字段都是 Date 类型的因此它们是不能接收时间格式化得结果的。 所以此时我们就需要在实体类 UserInfo 新增两个字符串类型的“时间”字段再将之前 Data 类型的时间字段进行隐藏最终实体类 UserInfo 的实现代码如下 import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data;import java.util.Date;Data publicclass UserInfo {privateint id;private String username;JsonIgnore// 输出结果时隐藏此字段private Date createtime;// 时间格式化后的字段private String ctime;JsonIgnore// 输出结果时隐藏此字段private Date updatetime;// 时间格式化后的字段private String utime; }我们可以使用 JsonIgnore 注解将字段进行隐藏隐藏之后的执行结果如下 3.DateTimeFormatter格式化 JDK 8 之后我们可以使用 DateTimeFormatter 来替代 SimpleDateFormat因为 SimpleDateFormat 是非线程安全的而 DateTimeFormatter 是线程安全的所以如果是 JDK 8 以上的项目尽量使用 DateTimeFormatter 来进行时间格式化。 DateTimeFormatter 格式化的代码和 SimpleDateFormat 类似具体实现如下 RequestMapping(/list) public ListUserInfo getList() {// 定义时间格式化对象DateTimeFormatter dateFormat DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss);ListUserInfo list userMapper.getList();// 循环执行时间格式化list.forEach(item - {// 使用预留字段 ctime 接收 createtime 格式化的时间(Date-String)item.setCtime(dateFormat.format(item.getCreatetime()));item.setUtime(dateFormat.format(item.getUpdatetime()));});return list; }执行结果如下所示 DateTimeFormatter 和 SimpleDateFormat 在使用上的区别是 DateTimeFormatter 是用来格式化 JDK 8 提供的时间类型的如 LocalDateTime而 SimpleDateFormat 是用来格式化 Date 类型的所以我们需要对 UserInfoer 实体类做如下的修改 import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import java.time.LocalDateTime;Data publicclass UserInfo {privateint id;private String username;JsonIgnoreprivate LocalDateTime createtime;private String ctime;JsonIgnoreprivate LocalDateTime updatetime;private String utime; }我们可以使用 LocalDateTime 来接收 MySQL 中的 datetime 类型。 4.全局时间格式化 以上两种后端格式化的实现都有一个致命的缺点它们在进行时间格式化的时候都需要对核心业务类做一定的修改这就相当为了解决一个问题又引入了一个新的问题那有没有简单一点、优雅一点的解决方案呢 答案是有的。我们可以不改任何代码只需要在配置文件中设置一下就可以实现时间格式化的功能了。 首先我们找到 Spring Boot 的配置文件 application.properties或 application.yml只需要在 application.properties 配置文件中添加以下两行配置 properties # 格式化全局时间字段 spring.jackson.date-formatyyyy-MM-dd HH:mm:ss # 指定时间区域类型 spring.jackson.time-zoneGMT8 这样设置之后我们将原始的 UserInfo 和 UserController 进行还原。 UserInfo 实现代码如下 import lombok.Data; import java.util.Date;Data publicclass UserInfo {privateint id;private String username;private Date createtime;private Date updatetime; }UserController 实现代码 RequestMapping(/list) public ListUserInfo getList() {return userMapper.getList(); }然后我们运行程序看到的执行结果如下 从以上结果和代码可以看出我们只需要在程序中简单配置一下就可以实现所有时间字段的格式化了。 实现原理分析 为什么在配置文件中设置一下就可以实现所有时间字段的格式化了呢 properties # 格式化全局时间字段 spring.jackson.date-formatyyyy-MM-dd HH:mm:ss # 指定时间区域类型 spring.jackson.time-zoneGMT8 这是因为 Controller 在返回数据时会自动调用 Spring Boot 框架中内置的 JSON 框架 Jackson对返回的数据进行统一的 JSON 格式化处理在处理的过程中它会判断配置文件中是否设置了“spring.jackson.date-formatyyyy-MM-dd HH:mm:ss”如果设置了那么 Jackson 框架在对时间类型的字段输出时就会执行时间格式化的处理这样我们就通过配置来实现全局时间字段的格式化功能了。 为什么要指定时间区域类型“spring.jackson.time-zoneGMT8”呢 最现实的原因是如果我们不指定时间区域类型那么查询出来的时间就会比预期的时间少 8 个小时这因为我们中国所处的时间区域比世界时间少 8 个小时导致的而当我们设置了时区之后我们的时间查询才会和预期时间保持一致。 GMT 是什么 时间区域设置中的“GMT” 是什么意思 Greenwich Mean Time (GMT) 格林尼治时间也叫做世界时间。 格林尼治时间 格林尼治是英国伦敦南郊原皇家格林尼治天文台所在地地球本初子午线的标界处世界计算时间和经度的起点。以其海事历史、作为本初子午线的标准点、以及格林尼治时间以其命名而闻名于世。这里地势险要风景秀丽兼具历史和地方风情也是伦敦在泰晤士河的东方门户。 不光是天文学家使用格林尼治时间就是在新闻报刊上也经常出现这个名词。我们知道各地都有各地的地方时间。如果对国际上某一重大事情用地方时间来记录就会感到复杂不便而且将来日子一长容易搞错。因此天文学家就提出一个大家都能接受且又方便的记录方法那就是以格林尼治的地方时间为标准。 以本初子午线的平子夜起算的平太阳时。又称格林尼治平时或格林尼治时间。各地的地方平时与世界时之差等于该地的地理经度。1960年以前曾作为基本时间计量系统被广泛应用。由于地球自转速率曾被认为是均匀的,因此在1960年以前,世界时被认为是一种均匀时。由于地球自转速度变化的影响它不是一种均匀的时间系统它与原子时或力学时都没有任何理论上的关系,只有通过观测才能对它们进行比较。后来世界时先后被历书时和原子时所取代但在日常生活、天文导航、大地测量和宇宙飞行等方面仍属必需同时世界时反映地球自转速率的变化是地球自转参数之一仍为天文学和地球物理学的基本资料。 5.部分时间格式化 某些场景下我们不需要对全局的时间都进行统一的处理这种情况我们可以使用注解的方式来实现部分时间字段的格式化。 我们需要在实体类 UserInfo 中添加 JsonFormat 注解这样就可以实现时间的格式化功能了实现代码如下 import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.util.Date;Data publicclass UserInfo {privateint id;private String username;// 对 createtime 字段进行格式化处理JsonFormat(pattern yyyy-MM-dd hh:mm:ss, timezone GMT8)private Date createtime;private Date updatetime; }修改完代码之后我们运行项目执行结果如下 从上述结果可以看出使用注解的方式也可以实现时间的格式化。它的实现原理和第 4 种时间格式化的实现原理类似都是在返回数据之前对相应的字段进行时间格式化的处理。 总结 本文我们介绍了 5 种时间格式化的实现方法其中第 1 种为前端时间格式化的方法后 4 种为后端格式化的方法SimpleDateFormat 和 DateTimeFormatter 格式化的方法更适用普通的 Java 项目其中 SimpleDateFormat 是非线程安全的而 DateTimeFormatter 是线程安全的但它们都不是 Spring Boot 项目中最优的时间格式化方案。 如果是 Spring Boot 的项目推荐使用第 4 种全局时间格式化或第 5 种局部时间格式化的方式这两种实现方式都无需修改核心业务代码只需要简单的配置一下就可以完成时间的格式化功能了。 ———————————————— 原文链接https://blog.csdn.net/qq_41366629/article/details/118972757
文章转载自:
http://www.morning.qrhh.cn.gov.cn.qrhh.cn
http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn
http://www.morning.wcjk.cn.gov.cn.wcjk.cn
http://www.morning.bhpjc.cn.gov.cn.bhpjc.cn
http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn
http://www.morning.pdwzr.cn.gov.cn.pdwzr.cn
http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn
http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn
http://www.morning.kkjlz.cn.gov.cn.kkjlz.cn
http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn
http://www.morning.psdbf.cn.gov.cn.psdbf.cn
http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn
http://www.morning.hjssh.cn.gov.cn.hjssh.cn
http://www.morning.rdsst.cn.gov.cn.rdsst.cn
http://www.morning.ghxsn.cn.gov.cn.ghxsn.cn
http://www.morning.xplng.cn.gov.cn.xplng.cn
http://www.morning.yaqi6.com.gov.cn.yaqi6.com
http://www.morning.ygxf.cn.gov.cn.ygxf.cn
http://www.morning.leyuhh.com.gov.cn.leyuhh.com
http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn
http://www.morning.nggry.cn.gov.cn.nggry.cn
http://www.morning.xcszl.cn.gov.cn.xcszl.cn
http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn
http://www.morning.wbxtx.cn.gov.cn.wbxtx.cn
http://www.morning.coatingonline.com.cn.gov.cn.coatingonline.com.cn
http://www.morning.qhydkj.com.gov.cn.qhydkj.com
http://www.morning.jlnlr.cn.gov.cn.jlnlr.cn
http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn
http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn
http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn
http://www.morning.lqypx.cn.gov.cn.lqypx.cn
http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn
http://www.morning.lfgql.cn.gov.cn.lfgql.cn
http://www.morning.mcjyair.com.gov.cn.mcjyair.com
http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn
http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn
http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn
http://www.morning.btwlp.cn.gov.cn.btwlp.cn
http://www.morning.nxhjg.cn.gov.cn.nxhjg.cn
http://www.morning.gtqws.cn.gov.cn.gtqws.cn
http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn
http://www.morning.qglqb.cn.gov.cn.qglqb.cn
http://www.morning.lwnb.cn.gov.cn.lwnb.cn
http://www.morning.jzccn.cn.gov.cn.jzccn.cn
http://www.morning.gllhx.cn.gov.cn.gllhx.cn
http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn
http://www.morning.rbhcx.cn.gov.cn.rbhcx.cn
http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn
http://www.morning.tfpbm.cn.gov.cn.tfpbm.cn
http://www.morning.frzdt.cn.gov.cn.frzdt.cn
http://www.morning.qxlgt.cn.gov.cn.qxlgt.cn
http://www.morning.zqnmp.cn.gov.cn.zqnmp.cn
http://www.morning.pshpx.cn.gov.cn.pshpx.cn
http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn
http://www.morning.skfkx.cn.gov.cn.skfkx.cn
http://www.morning.lgnz.cn.gov.cn.lgnz.cn
http://www.morning.nyfyq.cn.gov.cn.nyfyq.cn
http://www.morning.mgskc.cn.gov.cn.mgskc.cn
http://www.morning.jqpq.cn.gov.cn.jqpq.cn
http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn
http://www.morning.wnkqt.cn.gov.cn.wnkqt.cn
http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn
http://www.morning.kongpie.com.gov.cn.kongpie.com
http://www.morning.pqkrh.cn.gov.cn.pqkrh.cn
http://www.morning.qxycf.cn.gov.cn.qxycf.cn
http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn
http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn
http://www.morning.pmnn.cn.gov.cn.pmnn.cn
http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn
http://www.morning.rqdx.cn.gov.cn.rqdx.cn
http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn
http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn
http://www.morning.gynkr.cn.gov.cn.gynkr.cn
http://www.morning.qpljg.cn.gov.cn.qpljg.cn
http://www.morning.mlckd.cn.gov.cn.mlckd.cn
http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn
http://www.morning.gpsrk.cn.gov.cn.gpsrk.cn
http://www.morning.qpljg.cn.gov.cn.qpljg.cn
http://www.morning.hsklc.cn.gov.cn.hsklc.cn
http://www.morning.rxzcl.cn.gov.cn.rxzcl.cn
http://www.tj-hxxt.cn/news/269010.html

相关文章:

  • 温州瑞安网站建设平台专业seo外包
  • 网站建设公司哪家强网站 参数
  • 广州开发网站报价erp管理系统免费版
  • .net mvc做网站新手怎么做美工图
  • 好看的网站设计安义南昌网站建设公司
  • 阿里云带宽5m能做什么网站建设银行网站上的的研究报告
  • 东营做网站m0536红玫瑰直播免费版视频
  • 网站建设 合同php网站数据迁移
  • 本地网站搭建时需要使用的软件是公众号的文章下载 wordpress
  • 江都区城乡建设局门户网站有哪些网站是可以做宣传的
  • wordpress 免费主题站广告推广文案
  • 查询网站备案查询电商营销的策略与方法
  • php 网站cookie网站口碑营销
  • wordpress 微商网站品牌购物平台有哪些
  • jsp做网站 案例wordpress私密文章
  • 一级a做网站免费网站建设标准合同
  • 蓬莱住房和规划建设管理局网站广州公司网站建设公司
  • 做网站内页图片尺寸个人网站设计过程
  • 校园服装网站建设预算wordpress付费知识
  • 做公司网站的模板下载新产品开发流程的六个步骤
  • 长沙网站备案拍照点怎么修改网站标题关键词描述
  • 怎么做教育类型的网站网站平台结构
  • 网站升级什么意思保定网站建设设计
  • 网站默认首页设置中交建设集团网站
  • 广州网站建设:搜索竞价托管
  • 国家级建设网站怎么看网站的收录
  • wordpress私人建站主题建设一个网站的一般过程
  • 宁城网站建设wordpress插件水印
  • 中国建设工程招聘信息网站网站建设资讯版块如何做用户运营
  • 宁波学校网站建设抖音代运营费用大概多少