Skip to content

Instantly share code, notes, and snippets.

@apetresc
Created September 28, 2011 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save apetresc/1247128 to your computer and use it in GitHub Desktop.
Save apetresc/1247128 to your computer and use it in GitHub Desktop.
package com.evntcast.server.storm;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatterBuilder;
import backtype.storm.serialization.ISerialization;
public class DateTimeSerializer implements ISerialization<DateTime> {
private static final String DATE_FORMAT = "YYYY-MM-dd'T'HH:mm:ssZZ";
private static final int DATE_FORMAT_LENGTH = new DateTime().toString(DATE_FORMAT).length();
@SuppressWarnings("rawtypes")
@Override
public boolean accept(Class clazz) {
return clazz.getName().equals("org.joda.time.DateTime");
}
@Override
public DateTime deserialize(DataInputStream stream)
throws IOException {
byte[] buffer = new byte[DATE_FORMAT_LENGTH];
stream.read(buffer, 0, buffer.length);
return new DateTime( new String(buffer));
}
@Override
public void serialize(DateTime object, DataOutputStream stream)
throws IOException {
stream.write(object.toString(DATE_FORMAT).getBytes());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment