Jackson time format, time annotations @JsonFormat usage, jet lag problem description

Source: Internet
Author: User

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:

    1. ... ...
    2. /**更新时间 用户可以点击更新,保存最新更新的时间。**/
    3. @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
    4. private Date updateTime;
    5. ... ...

Add to getter Method:

  1. ... ...
  2. @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
  3. public Date getUpdateTime() {
  4. return updateTime;
  5. }
  6. ... ...

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:

    1. ... ...
    2. /** update time users can click Update to save the time of the latest update. **/
    3. @JsonFormat ( Pattern= "YYYY-MM-DD HH:mm:ss" ,timezone< Span class= "pun" >= "gmt+8" )
    4. private Date Updatetime
    5. ... ...

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.

  1. {
  2. "data": {
  3. "id": "11000002000016",
  4. "sitename": "新浪网",
  5. "sitedomain": "sina.com.cn",
  6. "sitetype": "交互式",
  7. "cdate": "2016-01-21",
  8. "comtype": "企业单位",
  9. "comname": "北京新浪互联信息服务有限公司",
  10. "comaddress": "北京市网安总队",
  11. "updateTime": "2017-09-05 02:26:34" //看这...这里就是刚刚输出的。
  12. },
  13. "status": 200
  14. }
  15. Transferred from: https://www.sojson.com/blog/246.html

Jackson time format, time annotations @JsonFormat usage, jet lag problem description

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.