By default Play Json just truncates time zone when working with ZonedDateTime from Java 8. So the following code:
case class MyClass(createdAt: ZonedDateTime)
implicit val myWrites = Json.writes[MyClass]
...
val d = MyClass(ZonedDateTime.parse("2015-10-01T12:13:14.00+02:00"))
val json = Json.toJson(d)
would produce following Json: {"createdAt": "2015-10-01T12:13:14"}
. That’s because play.api.libs.json.DefaultWrites.DefaultZonedDateTimeWrites
uses same formatter as DefaultLocalDateTimeWrites
and simply disregards time zone. To display the time zone together with date time you’ll need to add following code before myWrites
:
implicit val timeWrites: Writes[ZonedDateTime] =
Writes.temporalWrites[ZonedDateTime, DateTimeFormatter](DateTimeFormatter.ISO_DATE_TIME)
And then Json result will change to {"createdAt": "2015-10-01T12:13:14+02:00"}