当前位置:   article > 正文

@JsonFormat与@DateTimeFormat注解的使用_@dateformat

@dateformat

一、区别

注解@JsonFormat主要是后台前台的时间格式的转换

注解@DataFormAT主要是前台后台的时间格式的转换

二、@JsonFormat 的使用

@JsonFormat(pattern=“yyyy-MM-dd”,timezone = “GMT+8”)
pattern:是你需要转换的时间日期的格式
timezone:是时间设置为东八区,避免时间在转换中有误差

	public static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss";
  • 1

示例:

在这里插入图片描述

三、@DateTimeFormat 的使用

  • @DateTimeFormat 直接接收指定的时间格式,既灵活又方便。

在Controller中 @RequestParam和 @DateTimeFormat搭配使用

示例:
在这里插入图片描述

   @GetMapping("/getSceneMapByTimeToTime")
    public List<SceneMap> getSceneMapByTimeToTime(@RequestParam("startTime") @DateTimeFormat(pattern=Constants.DATEFORMAT)Date startTime,
                                                  @RequestParam("endTime") @DateTimeFormat(pattern=Constants.DATEFORMAT)Date endTime){
        return sceneMapService.getSceneMapByTimeToTime(startTime,endTime);
    }
  • 1
  • 2
  • 3
  • 4
  • 5

四、同时使用

在controller层我们使用spring mvc 表单自动封装映射对象时,我们在对应的接收前台数据的对象的属性上加
@JsonFormat与@DateTimeFormat注解
若既需要取数据到前台,也需要前台数据传到后台,都需要进行时间格式的转换,可以同时使用

@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date symstarttime;
 
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date symendtime;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/900302
推荐阅读
相关标签
  

闽ICP备14008679号