Skip to content

Instantly share code, notes, and snippets.

@robfletcher
Created August 12, 2009 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robfletcher/166335 to your computer and use it in GitHub Desktop.
Save robfletcher/166335 to your computer and use it in GitHub Desktop.
package com.energizedwork.grails.plugins.jodatime;
import grails.converters.JSON;
import org.codehaus.groovy.grails.web.converters.exceptions.ConverterException;
import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller;
import org.codehaus.groovy.grails.web.json.JSONException;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.util.Locale;
public class DateTimeMarshaller implements ObjectMarshaller<JSON> {
private final DateTimeFormatter JSON_DATE_TIME_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(DateTimeZone.UTC).withLocale(Locale.US);
public boolean supports(Object object) {
return object instanceof DateTime;
}
public void marshalObject(Object object, JSON converter) throws ConverterException {
try {
converter.getWriter().value(JSON_DATE_TIME_FORMAT.print((DateTime) object));
} catch (JSONException e) {
throw new ConverterException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment