Jackson-date Handling

Source: Internet
Author: User
Tags time zones

Handling dates on Java platform are complex business. Jackson tries not-make it any harder than it have to be. Here's how.

All time objects This has associated TimeZone (Java.util.Calendar etc) that Jackson constructs use the Standard timezone (GMT), not the local time zone (whatever-might be). That is Is:jackson defaults to the using GMT for all processing unless specifically told otherwise.

Date serialization

Why does Dates get written as numbers?

Default Serializer for Java.util.Date (and related, such as Java.util.Calendar) serialize them using the Most efficient accurate representation, so-called epoch timestamp (number of milliseconds since January 1st, 1970, UTC). Assumption is the if user does not care, we should use efficient and accurate representation to get job bdone.

One exception is java.sql.Date, which'll always being represent in textual form (but Note:use of Java.sql.Dat E is strongly discouraged due to multiple issues, see below for more details)

But I don ' t like that, I prefer more readable notation

No problem. The simplest-produce textual serialization is-to-use:

false);

Which disable use of timestamps (numbers), and instead use a [ISO-8601]-compliant notation, which gets output as Somethin G like: "1970-01-01t00:00:00.000+0000".

That is format sucks, can I use my own?

If you must. You can configure formatting by passing a Java.text.DateFormat instance like so:

// 1.8 and above  // For earlier versions (deprecated for 1.8+)

How come this time was off by 9 hours? (5 hours, 3 hours etc)

You are thinking in terms of your local time zone. Remember that Jackson defaults to using GMT (Greenwich time, one hour behind central European timezone; multiple hours Ahe Ad of US time zones).

Can I Configure it on per-property basis?

Yes, with Jackson 2.0 you can use the new @JsonFormat annotation:

 Public class Datestuff {    @JsonFormat (Shape=jsonformat.shape.string, pattern= "yyyy-mm-dd,hh:00", timezone= "CET" )    public  Date creationtime;}
Notes on Java.sql.Date

(aka "Please don't use java.sql.Date, ever!")

Although Jackson supports java.sql.Date, there is known issues with respect to timezone handling, partly due to Design of this class. It is recommended the this type is avoided, if possible, and regular java.util.Date (or java.util.calendar ) used instead. If This isn't possible, it may be necessary for applications to convert these dates using Java.util.Calendar and Explicit timezone definition.

Date deserialization

Which date formats is supported by default?

RFC-1123, ISO-8601 (or rather, commonly used subset). Also, numeric (integer) serializations is recognized, assumed to BES in Unix timestamp notation (milliseconds since epoch, Jan 1st 1970 GMT)

Can This is configured?

Yes:similar to serialization configuration, you can call:

Objectmapper.getdeserializationconfig (). Setdateformat (Mydateformat);
Interoperabilityjoda time

Starting with version 1.4, Jackson offers some support for Joda time data types:basically, its DateTime can a Utomatically serialized/deserialized similar to what java.util.Date is handled. More support can is added based on user requests.

With version 2.0, support for Joda date/time types is moved to a new module, Jackson-datatype-joda, to reduce dependencie S-Core DataBind has, as-well-to-make it easier to update joda-specific functionality. To use the module, you'll need to add the dependency (via Maven, or by including module Jar), and register module; t His would add full support similar to how Jackson 1.x works.

Jackson-date Handling

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.