In short, yes this is possible. With that said, I would encourage you to return a numeric representation of the date and leave it up to your consumers to display it however they want. Here is a means for achieving what you want.
Create a class that will serve as the Serializer for the Entity Object.
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
public class EntitySerializer extends JsonSerializer<Entity> {
@Override
public void serialize(Entity entity, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("name", entity.getName());
if (entity.getFieldThatIndicates24HourFormat()) {
jsonGenerator.writeStringField("date", entity.getDate().toString());
} else {
jsonGenerator.writeStringField("date", entity.getDate().toString());
}
jsonGenerator.writeEndObject();
}
}
On your Entity, add an annotation that will enable this class to be used for serializing it.
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonSerialize(using = EntitySerializer.class)
public class Entity {
There are obvious pitfalls with this, as you now will have to be mindful of changes to your Entity and update the serializer accordingly.