title: send and receive Date type with Avro on java
body: <p>Is there away I can send and receive Date type with <a href="http://avro.apache.org/" rel="nofollow noreferrer">Apache Avro</a>. I have not been able to find anything on this. Only things I found said that use int and logicalType of Date in schema. But that results in another int on the receiver side. I still have to convert it to date.</p>
  
  <p>I am trying to send date from a <a href="https://kafka.apache.org/" rel="nofollow noreferrer">Apache Kafka</a> producer and receive in the Kafka consumer.</p>
  
  <p>If there is not other way then do I have to convert date to int always and then back at the consumer. There is this article which shows how to do it:</p>
  
  <p><a href="https://stackoverflow.com/questions/6158053/get-the-number-of-days-weeks-and-months-since-epoch-in-java/35647105">Get the number of days, weeks, and months, since Epoch in Java</a></p>
  
  <p>Serializer code:-</p>
  
  <pre><code>@Override
      public byte[] serialize(String topic, T data) {
          try {
              byte[] result = null;
  
              if (data != null) {
                  logger.debug("data='{}'" +  data);
  
                  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                  BinaryEncoder binaryEncoder =
                          EncoderFactory.get().binaryEncoder(byteArrayOutputStream, null);
  
                  DatumWriter&lt;GenericRecord&gt; datumWriter = new GenericDatumWriter&lt;&gt;(data.getSchema());
                  datumWriter.write(data, binaryEncoder);
  
                  binaryEncoder.flush();
                  byteArrayOutputStream.close();
  
                  result = byteArrayOutputStream.toByteArray();
                  byteArrayOutputStream.close();
                  logger.debug("serialized data='{}'" +  DatatypeConverter.printHexBinary(result));
              }
              return result;
          } catch (IOException ex) {
              throw new SerializationException(
                      "Can't serialize data='" + data + "' for topic='" + topic + "'", ex);
          }
      }
  </code></pre>
  
  <p>desirializer code:-</p>
  
  <pre><code>    @Override
      public T deserialize(String topic, byte[] data) {
          try {
              T result = null;
  
              if (data != null) {
                  logger.debug("data='{}'" + DatatypeConverter.printHexBinary(data));
  
                  DatumReader&lt;GenericRecord&gt; datumReader =
                          new SpecificDatumReader&lt;&gt;(targetType.newInstance().getSchema());
                  Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);
  
                  result = (T) datumReader.read(null, decoder);
                  logger.debug("deserialized data='{}'" + result);                
              }
              return result;
          } catch (Exception ex) {
              throw new SerializationException(
                      "Can't deserialize data '" + Arrays.toString(data) + "' from topic '" + topic + "'", ex);
          }
      }
  </code></pre>
  
  <p>Schema file:-</p>
  
  <pre><code>{"namespace": "com.test",
    "type": "record",
    "name": "Measures",
    "fields": [  
      {"name": "transactionDate", "type": ["int", "null"], "logicalType" : "date" }
     ]
  }
  </code></pre>
  
  <p>and these two are just defined as serializer and deserializer classes in producer and consumer configuration.</p>