简体   繁体   中英

How to parse both Date and Time in using gson?

I have a json input that contains the following fields (among others):

...
leadDate: "2016-01-16",
leadTime: "13:45:22",
...

I'm trying to parse this json using Gson library to set them to the following Java attributes:

private Date leadDate;
private Time leadTime;

but the setDateFormat method doesn't allow me to set both date format and time format without the use of DateFormat constants. And these, as far as I know, doesn't include the formats I need.

What can I do if I have to use Gson?

No Date-Time Types In JSON

JSON lacks any date-time data types . So the format and meaning of your Strings is up to you and your data supplier/consumer.

Use java.time Classes

Avoid the old date-time classes bundled with the earliest versions of Java such as java.util.Date/.Calendar. They are notoriously troublesome. In Java 8 and later they have been officially supplanted by the java.time framework.

Converters

Gson has converters for serializers/deserializers to handle the chore of dehydrating and rehydrating java.time objects. See: Gson Type Adapters for Common Classes . Or consider writing your own . Such serialization tools would do something like the following.

Parsing

Your date and time are separate. Start by parsing each as LocalDate and LocalTime . Both your String inputs comply with the ISO 8601 standard defining formats of Strings representing date-time values. That is handy as the java.time classes use this standard as their defaults when parsing or generating such Strings.

LocalDate localDate = LocalDate.parse( "2016-01-16" );
LocalTime localTime = LocalTime.parse( "13:45:22" );

Time Zone

Both LocalDate and LocalTime are date-only and time-only respectively, and both lack any time zone or offset-from-UTC information. Such time zone information is critical to making sense of your date-time data. Does your example data mean a quarter until two in the afternoon of Paris, Montreal, or Tokyo? Perhaps you have additional fields of data with this zone/offset info, or perhaps in your context you can assume the time zone.

ZoneId zoneId = ZoneId.of( "America/Montreal" );

Let's apply that time zone to our date-only and time-only to get a full-fledged date-time, an actual moment on the timeline.

ZonedDateTime zdt = ZonedDateTime.of( localDate , localTime , zoneId );

String Output

You can output in ISO 8601 compliant string by simply calling toString . Actually, java.time extends the ISO 8601 format by appending the name of the time zone in brackets.

2016-01-16T13:45:22-05:00[America/Montreal]

To generate Strings in other formats, search StackOverflow.com for many examples of java.time.format package.

Instant

Generally the best practice is to do your business logic and data storage/exchange all in UTC . In java.time that means the Instant class which represents a moment on the timeline in UTC. We can instantiate an Instant object from our ZonedDateTime .

Instant instant = zdt.toInstant();

java.sql.Date and java.sql.Time have already built-in methods for doing it. Simply read the data from json file and pass it in valueOf method like this--

  1. In the case of leadDate-
    Date.valueOf(value of leadDate);

  2. In the case of leadTime-
    Time.valueOf(value of leadTime);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM