Created
January 9, 2012 23:19
-
-
Save ruckus/1585575 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @DatabaseTable(tableName="albums") | |
| public class Album { | |
| @JsonProperty("updated_at") | |
| @JsonSerialize(using=JodaDateTimeTimestampSerializer.class) | |
| @JsonDeserialize(using=JodaDateTimeTimestampDeserializer.class) | |
| @DatabaseField | |
| private long updatedAt; | |
| } | |
| // serializer | |
| public class JodaDateTimeTimestampSerializer extends JsonSerializer<DateTime> { | |
| @Override | |
| public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { | |
| jgen.writeNumber(DateUtil.dateTimeToString(value)); | |
| } | |
| } | |
| // deserializer | |
| public class JodaDateTimeTimestampDeserializer extends JsonDeserializer<Long> { | |
| @Override | |
| public Long deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { | |
| return(DateUtil.stringToLong(parser.getText())); | |
| } | |
| } | |
| // DateUtil | |
| public class DateUtil { | |
| public static String dateTimeToString(DateTime date) { | |
| return(date.toString(getFormatter())); | |
| } | |
| public static long stringToLong(String date) { | |
| DateTime parsed = getFormatter().parseDateTime(date); | |
| return(parsed.getMillis() / 1000); | |
| } | |
| private static DateTimeFormatter getFormatter() { | |
| return(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(DateTimeZone.forID("UTC"))); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment