title: How to serialize a Date using AVRO in Java
body: <p>I'm actually trying to serialize objects containing dates with Avro, and the deserialized date doesn't match the expected value (tested with avro 1.7.2 and 1.7.1). Here's the class I'm serializing :</p>
  
  <pre><code>import java.text.SimpleDateFormat;
  import java.util.Date;
  
  public class Dummy {
      private Date date;
      private SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
  
      public Dummy() {
      }
  
      public void setDate(Date date) {
          this.date = date;
      }
  
      public Date getDate() {
          return date;
      }
  
      @Override
      public String toString() {
          return df.format(date);
      }
  }
  </code></pre>
  
  <p>The code used to serialize / deserialize : </p>
  
  <pre><code>import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.util.Date;
  
  import org.apache.avro.Schema;
  import org.apache.avro.io.DatumReader;
  import org.apache.avro.io.DatumWriter;
  import org.apache.avro.io.Decoder;
  import org.apache.avro.io.DecoderFactory;
  import org.apache.avro.io.Encoder;
  import org.apache.avro.io.EncoderFactory;
  import org.apache.avro.reflect.ReflectData;
  import org.apache.avro.reflect.ReflectDatumReader;
  import org.apache.avro.reflect.ReflectDatumWriter;
  
  public class AvroSerialization {
  
      public static void main(String[] args) {
          Dummy expected = new Dummy();
          expected.setDate(new Date());
          System.out.println("EXPECTED: " + expected);
          Schema schema = ReflectData.get().getSchema(Dummy.class);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          Encoder encoder = EncoderFactory.get().binaryEncoder(baos, null);
          DatumWriter&lt;Dummy&gt; writer = new ReflectDatumWriter&lt;Dummy&gt;(schema);
          try {
              writer.write(expected, encoder);
              encoder.flush();
              Decoder decoder = DecoderFactory.get().binaryDecoder(baos.toByteArray(), null);
              DatumReader&lt;Dummy&gt; reader = new ReflectDatumReader&lt;Dummy&gt;(schema);
              Dummy actual = reader.read(null, decoder);
              System.out.println("ACTUAL: " + actual);
          } catch (IOException e) {
              System.err.println("IOException: " + e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>And the output :</p>
  
  <pre><code>EXPECTED: 06/11/2012 05:43:29.188
  ACTUAL: 06/11/2012 05:43:29.387
  </code></pre>
  
  <p>Is it related to a known bug, or is it related to the way I'm serializing the object ?</p>