-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleDateConverter.java
More file actions
31 lines (26 loc) · 890 Bytes
/
ExampleDateConverter.java
File metadata and controls
31 lines (26 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package io.tomahawkd.gson;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExampleDateConverter implements JsonSerializer<Date>, JsonDeserializer<Date> {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String date = json.getAsString().replace("\"", "");
try {
return formatter.parse(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return context.serialize(formatter.format(src));
}
}