Created
November 13, 2012 09:00
-
-
Save jasoet/4064739 to your computer and use it in GitHub Desktop.
Serialize Deserialize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Deny Prasetyo,S.T | |
* Java(Script) and Rails Developer | |
* jasoet87@gmail.com | |
* <p/> | |
* http://github.com/jasoet | |
* http://bitbucket.com/jasoet | |
* | |
* @jasoet | |
*/ | |
public class JsonDateDeserializer extends JsonDeserializer<Date> { | |
@Override | |
public Date deserialize(JsonParser jsonparser, | |
DeserializationContext deserializationcontext) throws IOException, JsonProcessingException { | |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); | |
String date = jsonparser.getText(); | |
try { | |
return format.parse(date); | |
} catch (ParseException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
/** | |
* Created by Deny Prasetyo,S.T | |
* Java(Script) and Rails Developer | |
* jasoet87@gmail.com | |
* <p/> | |
* http://github.com/jasoet | |
* http://bitbucket.com/jasoet | |
* | |
* @jasoet | |
*/ | |
public class JsonDateSerializer extends JsonSerializer<Date> { | |
@Override | |
public void serialize(Date value, JsonGenerator gen, SerializerProvider arguments) throws IOException, JsonProcessingException { | |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); | |
String formattedDate = formatter.format(value); | |
gen.writeString(formattedDate); | |
} | |
} | |
//Penggunaan | |
@Column(name = "created_at") | |
@Temporal(value = TemporalType.TIMESTAMP) | |
@JsonSerialize(using = JsonDateSerializer.class) | |
@JsonDeserialize(using = JsonDateDeserializer.class) | |
private Date createdAt; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment