2

I'm using JSON-B (yasson implementation) and I'm receiving data for an object with a field like this

{
  ...
  "timestamp": "2020-03-19T06:42:42Z",
  ...
}

which is perfectly standard ISO 8601 for a UTC date-time value. Now the corresponding Java class simply declares a Date member variable with no other specific annotation

...
 private Date timestamp;
...

everything seems to work fine and it looks like the JSON-B implementation correctly understands that as UTC without me having to specify a format using the @JsonbDateFormat annotation. I think I'm sure of it because I checked with

ZonedDateTime datetimeCheck = ZonedDateTime.of(2020, 3, 19, 6, 42, 42, 0, ZoneId.of("UTC"));
Date parsedDateFromJson = myModel.getTimestamp();
boolean compareTs = parsedDateFromJson.equals(Date.from(datetimeCheck.toInstant()));

and it yields true however, when I ran another test removing the 'Z' from the date-time value I was expecting it to produce a different result interpreting the date-time value as local rather than UTC. With my great surprise, the Date object obtained by JSON-B was exactly the same. What am I missing here? Why are 2020-03-19T06:42:42Z and 2020-03-19T06:42:42 the same thing? (I don't think they are). Or maybe is the JSON-B implementation considering UTC as default always when no timezone is specified?

Thanks

  • Ok. How did you configure your JSON-B implementation ? I am asking because I can't do it in a way to bind both `2020-08-047T19:07:41Z` and `2020-08-047T19:07:41` to `java.util.Date` ? Mine is failing with the former. – EvgeniySharapov Aug 06 '20 at 14:33

1 Answers1

0

Or maybe is the JSON-B implementation considering UTC as default always when no timezone is specified?

Exactly this. Contrary to what its name implies, a java.util.Date actually doesn't represent a date but an instant in time. Specifically, it represents the number of milliseconds since UNIX epoch (January 1, 1970, 00:00:00 GMT).

2020-03-19T06:42:42 is a date + time without zone or offset information. When deserializing this date + time to a java.util.Date, i.e. an instant in time, you need to decide how to interpret the date + time. Is this the date + time at UTC? Is this the date + time in your local timezone?

Luckily for us, the JSON-B specification contains the following: "If not specified otherwise in this section, GMT standard time zone and offset specified from UTC Greenwich is used." As this statement shows, the authors made the choice of interpreting a date + time without timezone as a date + time at UTC.

Martijn
  • 5,491
  • 4
  • 33
  • 41
  • Thanks I missed that part in the spec. Could you please provide the exact location where you've read it? Many thanks. I think it's an important part it should be more visible and clear – Marco Del Percio Mar 30 '20 at 07:43
  • The spec is maintained on GitHub, you can find that exact sentence at: https://github.com/eclipse-ee4j/jsonb-api/blob/master/spec/src/main/asciidoc/jsonb.adoc – Martijn Mar 30 '20 at 08:21