title: Jackson custom date serializer
body: <p>I need to set format for class' date serialization.
  I have the version of Jackson, which doesn't have @JsonFormat. That's Why I wrote custom class:</p>
  
  <pre><code>public class CDJsonDateSerializer extends JsonSerializer&lt;Date&gt;{
  
  @Override
  public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
      SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
      String dateString = dateFormat.format(date);
      jsonGenerator.writeString(dateString);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And used it:</p>
  
  <pre><code>@JsonSerialize(using = CDJsonDateSerializer.class)
  private Date startDate;
  </code></pre>
  
  <p>But, I have another fields which have different date's formats and I don't want to create another classes for serialization. Can I add all needed formats like constants to CDJsonDateSerializer class and set needed format with annotation <code>@JsonSerialize</code> ?
  Something like this: </p>
  
  <p><code>@JsonSerialize(using = CDJsonDateSerializer.class, CDJsonDateSerializer.FIRST_FORMAT)</code>.</p>
  
  <p><strong>AFTER THE ANSWER BELOW:</strong></p>
  
  <p>It works after some corrections. I've changed the way of getting annotation in <strong>createContextual</strong> method:</p>
  
  <pre><code>@Override
  public JsonSerializer createContextual(SerializationConfig serializationConfig, BeanProperty beanProperty) {
      return new CustomDateSerializer(beanProperty.getAnnotation(JsonDateFormat.class).value());
  }
  </code></pre>
  
  <p>And I've added <strong>@JacksonAnnotation</strong> to my created new annotation JsonDateFormat:</p>
  
  <pre><code>@Retention(RetentionPolicy.RUNTIME)
  @JacksonAnnotation
  public @interface JsonDateFormat {
     String value();
  }
  </code></pre>