How can I parse LoaclDateTime to ISO 8601 String?

176 views
Skip to first unread message

Igor Wojda

unread,
Mar 10, 2023, 2:28:42 PM3/10/23
to jackson-user
Hi

How can I parse LoaclDateTime to ISO 8601 format?

Scenario:
val data class task(val name: String, val loaclDateTime: LoaclDateTime)

val objectMapper = ObjectMapper()
objectMapper.registerModule(JavaTimeModule())
val task = Task("task", LocalDateTime.now())
val json = objectMapper.writeValueAsString(jsonBody)

Actual
{
"name":"task"
,"dueDate":[2023,3,11,15,51,49,540250000]
}

Expected
{
"name":"task"
,"dueDate":""2023-03-11T15:41:49.540"
}


Tatu Saloranta

unread,
Mar 10, 2023, 9:33:53 PM3/10/23
to jackso...@googlegroups.com
On Fri, Mar 10, 2023 at 11:28 AM Igor Wojda <igor....@gmail.com> wrote:
>
> Hi
>
> How can I parse LoaclDateTime to ISO 8601 format?

I think your question is actually "how do I WRITE (serialize)
LocalDateTime as JSON String using ISO-8601 format".
Parsing would mean reading JSON String into LocalDateTime, which
probably works fine.
Now, by default date/time values are written as timestamps (numbers);
you need to configure handling like so:

>
> Scenario:
> val data class task(val name: String, val loaclDateTime: LoaclDateTime)
>
> val objectMapper = ObjectMapper()

objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

and then output should contain ISO-8601 formatted JSON String instead of number.

Hope this helps,

-+ Tatu +-

Igor Wojda

unread,
Mar 13, 2023, 10:56:24 AM3/13/23
to jackson-user
Exactly.
Thx Tatu 

Reply all
Reply to author
Forward
0 new messages