@JsonFormat注解是一个时间格式化注解,比如我们存储在mysql中的数据是date类型的,当我们读取出来封装在实体类中的时候,就会变成英文时间格式,而不是yyyy-MM-dd HH:mm:ss这样的中文时间,因此我们需要用到JsonFormat注解来格式化我们的时间。

JsonFormat注解是jackson包里面的一个注解,因此在使用的时候需要引入fasterxml maven的jar包,如下所示。

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>

引入fasterxml maven jar包之后,就可以在实体类属性上面使用@JsonFormat注解了,要注意的是,它只会在类似@ResponseBody返回json数据的时候,才会返回格式化的yyyy-MM-dd HH:mm:ss时间,你直接使用System.out.println()输出的话,仍然是类似“Fri Dec 01 21:05:20 CST 2017”这样的时间样式。

package demo;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat; public class Student {
private int id;
private String username; @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date createDate; //getter setter省略。。。 }

当我们这样@ResponseBody输出json数据的时候,@JsonFormat注解标识的date属性就会自动返回yyyy-MM-dd HH:mm:ss样式的时间了,例如。

@PostMapping("/api/getStudent")
@ResponseBody
public Map<String,Object> findStudentById(Long stuId){
Map<String,Object> resultMap = new HashMap<>();
Student stu = StudentService.findStudentById(stuId);
resultMap.put("result",stu);
return resultMap;
}

@JsonFormat时间格式化注解使用的更多相关文章

  1. js 中时间格式化的几种方法

    1.项目中时间返回值,很过时候为毫秒值,我们需要转换成 能够看懂的时间的格式: 例如: yyyy-MM-dd HH:mm:ss 2.处理方法(处理方法有多种,可以传值到前端处理,也可以后台可以好之后再 ...

  2. Springboot 关于日期时间格式化处理方式总结

    项目中使用LocalDateTime系列作为DTO中时间的数据类型,但是SpringMVC收到参数后总报错,为了配置全局时间类型转换,尝试了如下处理方式. 注:本文基于Springboot2.x测试, ...

  3. 3种 Springboot 全局时间格式化方式,别再写重复代码了

    本文收录在个人博客:www.chengxy-nds.top,技术资料共享,同进步 时间格式化在项目中使用频率是非常高的,当我们的 API 接口返回结果,需要对其中某一个 date 字段属性进行特殊的格 ...

  4. SpringBoot中时间格式化的5种方法!

    在我们日常工作中,时间格式化是一件经常遇到的事儿,所以本文我们就来盘点一下 Spring Boot 中时间格式化的几种方法. ​ 时间问题演示 为了方便演示,我写了一个简单 Spring Boot 项 ...

  5. SpringMVC的数据格式化-注解驱动的属性格式化

    一.什么是注解驱动的属性格式化? --在bean的属性中设置,SpringMVC处理 方法参数绑定数据.模型数据输出时自动通过注解应用格式化的功能. 二.注解类型 1.DateTimeFormat @ ...

  6. Spring格式化注解

    Spring Framework 3.0发布了.这里我们介绍其中的一个:用于格式化的注解.简介 Spring 3 提供了两个可以用于格式化数字.日期和时间的注解@NumberFormat和@DateT ...

  7. 【记录】Mybatis Generator生成数据对象Date/TimeStamp 查询时间格式化

    Mybatis Generator是很好的工具帮助我们生成表映射关联代码,最近博主遇到一个问题,找了很久才解决, 就是用Mybatis Generator生成实体类的时候,Date 时间无法格式化输出 ...

  8. strftime 日期时间格式化

    strftime() 函数根据区域设置格式化本地时间/日期,函数的功能将时间格式化,或者说格式化一个时间字符串. size_t strftime(char *strDest,size_t maxsiz ...

  9. javascript 时间格式化

    添加扩展 //时间格式化扩展Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1 ...

随机推荐

  1. Go语言学习笔记(六)net & net/http

    加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 net import "net" net包提供了可移植的网络I/O接口,包括TCP/IP.UD ...

  2. MySQL运维之--xtrabackup工具的原理和使用方法

    Xtrabackup工具的介绍 xtrabackup是percona公司开发的一款自由.免费.开源的一款备份工具,他的备份特点就是:支持热备.备份速度快. xtrabackup包含两个重要的工具:in ...

  3. OpenCV学习参考 即时贴

    注意:本博文在github上日常更新(保持GitHub最新) https://github.com/SylvesterLi/MyOpenCVCode 基本安装:https://blog.csdn.ne ...

  4. C++中const和指针

    常见的理解问题: const char * * s;//表示s是指向const char * 类型的指针: char * * const s;//表示s是指向char * 类型的一个常量指针.

  5. October 19th 2017 Week 42nd Thursday

    If you remember me, then I don't care if everyone else forgets. 只要你记得我,我不介意整个世界都把我遗忘了. I can't follo ...

  6. python基本语法:

    http://www.runoob.com/python/python-basic-syntax.html

  7. http-server

    http-server是基于node.js的一个简单.零配置的命令行web服务器,可以方便实现跨域资源请求, #全局安装: npm install http-server -g: 全局安装后就可以通过 ...

  8. jQuery UI dialog插件出错信息:$(this).dialog is not a function

    使用jQuery UI 1.7.2 dialog插件,遇到这样的错误: [img]http://dl.iteye.com/upload/attachment/308166/97c428e4-2ce2- ...

  9. ftp传输文件到指定服务器

    #!/bin/bash filePrefix="dbname"localDir="/DBBackup"remoteDir="/Backup" ...

  10. 使用 rem 作为单位使页面自适应设备宽度

    一.新建 rem.js 文件,代码如下: export default function () { document.documentElement.style.fontSize = document ...