Reputation: 5776
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?
Upvotes: 3
Views: 2130
Reputation: 338775
JSON lacks any date-time data types. So the format and meaning of your Strings is up to you and your data supplier/consumer.
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.
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.
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" );
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 );
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();
Upvotes: 2
Reputation: 222
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--
In the case of leadDate-
Date.valueOf(value of leadDate);
In the case of leadTime-
Time.valueOf(value of leadTime);
Upvotes: 0