Jackson was the official recommendation of SPRINGMVC, in fact I was used to Gson, but because of the company's unified use of Jackson, the nature of Jackson need to pay more attention. Let's say one of the annotations is @JsonFormat.
@JsonFormat use
We can have two usages (I know), on the object property, or on the method of the property, getter
as shown in the following code:
Add to attribute:
... ...
/**更新时间 用户可以点击更新,保存最新更新的时间。**/
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date updateTime;
... ...
Add to getter
Method:
... ...
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
public Date getUpdateTime() {
return updateTime;
}
... ...
The output of the above results is the same. This is nothing to explain. Specific output format, self-tuning pattern
.
@JsonFormat 8-hour difference.
The above use directly, in our China and our Beijing time, will be 8 hours, because we are East eight (Beijing time).
So we want to specify the time zone () when formatting timezone
, the code is as follows:
-
... ...
-
-
/** update time users can click Update to save the time of the latest update. **/
-
@JsonFormat ( Pattern= "YYYY-MM-DD HH:mm:ss" ,timezone< Span class= "pun" >= "gmt+8" )
-
private Date Updatetime
-
-
... ...
That is, add a property, timezone="GMT+8"
you can, getter
method I will not write, the same.
Let me see the result, I this interface is so output: public security Network to prepare queries, http://www.sojson.com/api/gongan/sina.com.cn
as an example.
{
"data": {
"id": "11000002000016",
"sitename": "新浪网",
"sitedomain": "sina.com.cn",
"sitetype": "交互式",
"cdate": "2016-01-21",
"comtype": "企业单位",
"comname": "北京新浪互联信息服务有限公司",
"comaddress": "北京市网安总队",
"updateTime": "2017-09-05 02:26:34" //看这...这里就是刚刚输出的。
},
"status": 200
}
- Transferred from: https://www.sojson.com/blog/246.html
Jackson time format, time annotations @JsonFormat usage, jet lag problem description