Java tutorial
/* * Carrot - beacon content management * Copyright (C) 2016 Heiko Dreyer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.boxedfolder.carrot.domain.util; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; import java.io.IOException; /** * @author Heiko Dreyer (heiko@boxedfolder.com) */ public class DateTimeDeserializer extends JsonDeserializer<DateTime> { @Override public DateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { DateTime dateTime = null; try { JsonToken currentToken = jsonParser.getCurrentToken(); System.out.println(jsonParser.getText()); if (currentToken == JsonToken.VALUE_STRING) { String dateTimeAsString = jsonParser.getText().trim(); DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withZoneUTC(); dateTime = formatter.parseDateTime(dateTimeAsString); } } catch (Exception e) { throw deserializationContext.mappingException(getClass()); } return dateTime; } }