001package io.konik.sdk.json;
002
003import java.io.IOException;
004import java.text.DateFormat;
005import java.text.ParseException;
006import java.text.SimpleDateFormat;
007import java.util.Date;
008
009import com.fasterxml.jackson.core.JsonParser;
010import com.fasterxml.jackson.core.JsonProcessingException;
011import com.fasterxml.jackson.databind.DeserializationContext;
012import com.fasterxml.jackson.databind.JsonDeserializer;
013
014public class DateTimeDeserializer extends JsonDeserializer<Date> {
015   private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");
016
017   @Override
018   public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
019         throws IOException, JsonProcessingException {
020      String dateAsString = jsonParser.readValueAs(String.class);
021      Date date = null;
022      try {
023         date = DATE_FORMAT.parse(dateAsString);
024      } catch (ParseException e) {
025         throw new RuntimeException(e);
026      }
027
028      return date;
029   }
030}