Open
Description
(This issue is about the same problem as #92. However, I decided to create it because the old issue describes the problem in an indirect way, and because WRITE_DATES_WITH_CONTEXT_TIME_ZONE
was added to https://github.com/FasterXML/jackson-databind since then.)
In version 2.18.1, DateTime
instances are always serialized using ObjectMapper
's time zone (even if it was not explicitly specified - UTC
is used in this case), rather than with DateTime
's own zone. Disabling the WRITE_DATES_WITH_CONTEXT_TIME_ZONE
serialization feature (which was added for FasterXML/jackson-modules-java8#222 to address the same problem but with JSR-310 date-time types) does not help.
Test code:
package local;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import org.joda.time.*;
public class App
{
public static void main(String[] args) throws Exception
{
DateTime dateTime = DateTime.parse("2024-12-01T00:00:00+02:00");
System.out.println("DateTime: " + dateTime);
ObjectMapper mapper = _objectMapper();
String json = mapper.writeValueAsString(dateTime);
System.out.println("Serialized: " + json);
}
private static ObjectMapper _objectMapper()
{
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE, false);
return mapper;
}
}
Output:
DateTime: 2024-12-01T00:00:00.000+02:00
Serialized: "2024-11-30T22:00:00.000Z"
Expected:
DateTime: 2024-12-01T00:00:00.000+02:00
Serialized: "2024-12-01T00:00:00.000+02:00"