From 31f47e58fc1e41d1a296a563040558ffc14a129f Mon Sep 17 00:00:00 2001 From: Dobler Patrick BIT Date: Mon, 2 Sep 2013 16:36:18 +0200 Subject: [PATCH] Issue #21: DateTimeSerializer use the configured time zone --- .../datatype/joda/ser/DateTimeSerializer.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/datatype/joda/ser/DateTimeSerializer.java b/src/main/java/com/fasterxml/jackson/datatype/joda/ser/DateTimeSerializer.java index 1f650fe4..379ed71d 100644 --- a/src/main/java/com/fasterxml/jackson/datatype/joda/ser/DateTimeSerializer.java +++ b/src/main/java/com/fasterxml/jackson/datatype/joda/ser/DateTimeSerializer.java @@ -9,12 +9,17 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializerProvider; +import org.joda.time.DateTimeZone; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; public final class DateTimeSerializer extends JodaSerializerBase { public DateTimeSerializer() { super(DateTime.class); } + private DateTimeFormatter format = null; + @Override public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException @@ -22,7 +27,7 @@ public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider pro if (provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) { jgen.writeNumber(value.getMillis()); } else { - jgen.writeString(value.toString()); + jgen.writeString(getDateTimeFormatter(provider).print(value)); } } @@ -32,4 +37,11 @@ public JsonNode getSchema(SerializerProvider provider, java.lang.reflect.Type ty return createSchemaNode(provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) ? "number" : "string", true); } + + private DateTimeFormatter getDateTimeFormatter(SerializerProvider provider) { + if (format == null) { + format = ISODateTimeFormat.dateTime().withZone(DateTimeZone.forTimeZone(provider.getTimeZone())); + } + return format; + } } \ No newline at end of file