Skip to content

Instantly share code, notes, and snippets.

@jasoet
Created November 13, 2012 09:00
Show Gist options
  • Save jasoet/4064739 to your computer and use it in GitHub Desktop.
Save jasoet/4064739 to your computer and use it in GitHub Desktop.
Serialize Deserialize
/**
* 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