mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Json Timestamp parsing - Add support for long values
This commit is contained in:
+41
-33
@@ -9,43 +9,51 @@ import com.avaje.ebean.text.json.JsonValueAdapter;
|
||||
|
||||
public class DefaultJsonValueAdapter implements JsonValueAdapter {
|
||||
|
||||
private final SimpleDateFormat dateTimeProto;
|
||||
private final SimpleDateFormat dateTimeProto;
|
||||
|
||||
public DefaultJsonValueAdapter(String dateTimeFormat){
|
||||
this.dateTimeProto = new SimpleDateFormat(dateTimeFormat);
|
||||
this.dateTimeProto.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
}
|
||||
|
||||
public DefaultJsonValueAdapter(){
|
||||
this("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
||||
}
|
||||
|
||||
private SimpleDateFormat dtFormat() {
|
||||
return (SimpleDateFormat)dateTimeProto.clone();
|
||||
}
|
||||
|
||||
public String jsonFromDate(Date date) {
|
||||
return "\""+date.toString()+"\"";
|
||||
}
|
||||
public DefaultJsonValueAdapter(String dateTimeFormat) {
|
||||
this.dateTimeProto = new SimpleDateFormat(dateTimeFormat);
|
||||
this.dateTimeProto.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
}
|
||||
|
||||
public String jsonFromTimestamp(Timestamp date) {
|
||||
return "\""+dtFormat().format(date)+"\"";
|
||||
}
|
||||
public DefaultJsonValueAdapter() {
|
||||
this("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
||||
}
|
||||
|
||||
public Date jsonToDate(String jsonDate) {
|
||||
return Date.valueOf(jsonDate);
|
||||
}
|
||||
private SimpleDateFormat dtFormat() {
|
||||
return (SimpleDateFormat) dateTimeProto.clone();
|
||||
}
|
||||
|
||||
public Timestamp jsonToTimestamp(String jsonDateTime) {
|
||||
try {
|
||||
java.util.Date d = dtFormat().parse(jsonDateTime);
|
||||
return new Timestamp(d.getTime());
|
||||
} catch (Exception e) {
|
||||
String m = "Error parsing Datetime["+jsonDateTime+"]";
|
||||
throw new RuntimeException(m, e);
|
||||
}
|
||||
public String jsonFromDate(Date date) {
|
||||
return "\"" + date.toString() + "\"";
|
||||
}
|
||||
|
||||
public String jsonFromTimestamp(Timestamp date) {
|
||||
return "\"" + dtFormat().format(date) + "\"";
|
||||
}
|
||||
|
||||
public Date jsonToDate(String jsonDate) {
|
||||
try {
|
||||
long utc = Long.parseLong(jsonDate);
|
||||
return new java.sql.Date(utc);
|
||||
} catch (NumberFormatException ex) {
|
||||
return Date.valueOf(jsonDate);
|
||||
}
|
||||
}
|
||||
|
||||
public Timestamp jsonToTimestamp(String jsonDateTime) {
|
||||
try {
|
||||
long utc = Long.parseLong(jsonDateTime);
|
||||
return new Timestamp(utc);
|
||||
} catch (NumberFormatException ex) {
|
||||
try {
|
||||
java.util.Date d = dtFormat().parse(jsonDateTime);
|
||||
return new Timestamp(d.getTime());
|
||||
} catch (Exception e) {
|
||||
String m = "Error parsing Datetime[" + jsonDateTime + "]";
|
||||
throw new RuntimeException(m, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user