Jackson2.1.4 序列化格式化时间
public class User {
private int id;
private Date birthday;
private double money;
private String name;
public User() {
}
public User(int id, String name, Date birthday) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
}
public User(int id, String name, double money, Date birthday) {
super();
this.id = id;
this.name = name;
this.money = money;
this.birthday = birthday;
}
public Date getBirthday() {
return birthday;
}
public int getId() {
return id;
}
public double getMoney() {
return money;
}
public String getName() {
return name;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setId(int id) {
this.id = id;
}
public void setMoney(double money) {
this.money = money;
}
public void setName(String name) {
this.name = name;
}
}
结果{"id":1,"name":"JACK","money":0.0,"birthday":"2013-04-01"}
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,FALSE);
禁止使用时间戳(数字),而使用[ISO-8601标准的符号,它得到类似的输出:“1970-01-01T00:00:00.000 +0000”。
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
也可以再类的属性上自定义哪些属性需要自定义序列化
timezone="GMT+8" 这里是中国的时区 东8区
自定义格式化类
JsonSerializer<T>
@JsonSerialize(user=xxx.class)
ObjectMapper mapper = new ObjectMapper();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
mapper.setDateFormat(format);
User user = new User(1,"JACK",new Date());
String outJson = mapper.writeValueAsString(user);
System.out.println(outJson);
Notes on java.sql.Date (官方不建议使用此类型)
(aka "Please do NOT use java.sql.Date, ever!")
Although Jackson supports java.sql.Date, there are known issues with respect to timezone handling, partly due to design of this class. It is recommended that this type is avoided, if possible, and regular java.util.Date (orjava.util.Calendar) used instead. If this is not possible, it may be necessary for applications to convert these dates using java.util.Calendar and explicit timezone definition.
Jackson2.1.4 序列化格式化时间的更多相关文章
- 在SpringMVC中使用Jackson并格式化时间
在spring MVC 3中,要实现REST风格的JSON服务,最简单的方式是使用 @ResponseBody 注解.该注解会自动把返回的对象,序列化为JSON. 来看一个最简单的例子.这个例子先使用 ...
- SpringBoot中使用Fastjson/Jackson对JSON序列化格式化输出的若干问题
来源 :https://my.oschina.net/Adven/blog/3036567 使用springboot-web编写rest接口,接口需要返回json数据,目前国内比较常用的fastjso ...
- JavaScriptSerializer 序列化json 时间格式
利用JavaScriptSerializer 序列化json 时间格式,得到的DateTime值值显示为“/Date(700000+0500)/”形式的JSON字符串,显然要进行转换 1.利用字符串直 ...
- WP7、WP8 格式化时间为距当前多少时间
方法一: 使用 toolkit的 RelativeTimeConverter,使用方式 <phone:PhoneApplicationPage.Resources> <toolkit ...
- golang 格式化时间为字符串
package main import ( "fmt" "reflect" "time" ) func main() { //格式化字符串为 ...
- php友好格式化时间
php格式化时间显示 function toTime($time) {//$time必须为时间戳 $rtime = date("Y-m-d H:i",$time); $htime ...
- java 格式化时间
java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); form ...
- 封装insertAfter、addClass、格式化时间
insertAfter,在JS节点操作中,并没有insertAfter方法,因此需要重新封装 function insertAfter(newEle,targetNode) { var oParent ...
- Jquery 格式化时间
我们常常会通过datetime得到时间,但是网页前台往往会显示不同的时间 如:2013-12-15 2013年12月23日 2013 12 15 等多种显示效果,这就需要我们把时间格式化一下. 下面是 ...
随机推荐
- RabbitMQ概念及环境搭建(三)RabbitMQ cluster
测试环境:VMS00781 VMS00782 VMS00386 (centos5.8) 1.先在三台机器上分别安装RabbitMQ Server 2.读取其中一个节点的cookie,并复制到其他节点( ...
- 理解over()函数
1.1.两个order by的执行时机分析函数(以及与其配合的开窗函数over())是在整个sql查询结束后(sql语句中的order by的执行比较特殊)再进行的操作, 也就是说sql语句中的ord ...
- PLSQL_标准删除的方式Delete/Drop/Truncate区别和比较(概念)
2014-06-02 Created By BaoXinjian
- Concurrency Managed Workqueue(三)创建workqueue代码分析
一.前言 本文主要以__alloc_workqueue_key函数为主线,描述CMWQ中的创建一个workqueue实例的代码过程. 二.WQ_POWER_EFFICIENT的处理 __alloc_w ...
- Python3 列表 clear() 方法
描述 Python3 列表 clear() 方法用于清空列表,类似于 del a[:]. 语法 clear() 方法语法: L.clear() 参数 无. 返回值 该方法没有返回值. 实例 以下实例展 ...
- Python isnumeric() 方法
描述 Python isnumeric() 方法检测字符串是否只由数字组成.这种方法是只针对unicode对象. 注:定义一个字符串为Unicode,只需要在字符串前添加 'u' 前缀即可,具体可以查 ...
- Python isalpha() 方法
描述 Python isalpha() 方法检测字符串是否只由字母或汉字组成. 语法 isalpha() 方法语法: S.isalpha() 参数 无. 返回值 如果字符串至少有一个字符并且所有字符都 ...
- js判断字符串str是否包含字符串substr
js判断字符串str是否包含字符串substr: function addUser(id,realName){ var userids = $("#userids").val(); ...
- Xilinx全局时钟
前言 Xilinx系列.ISE环境中,设计复杂工程时全局时钟系统的设计显得尤为重要. 一.时钟网络与全局缓冲 在XilinxFPGA中,时钟网络分为两类:全局时钟网络和I/O区域时钟网络.以全铜工艺实 ...
- <二>读<<大话设计模式>>之策略模式
又和大家见面了.可以坚持写出第二篇文章真不错,好好加油. <<大话设计模式>>解说策略模式是以商场收银软件程序开头的,那么问题来了.哪家商场收银软件强,开玩笑了. 读过上篇文章 ...