mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1724 - Fix, UtilDateTimeParser - date time parsing with nanos precision
This commit is contained in:
@@ -1,57 +1,28 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeFormatterBuilder;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
class UtilDateTimeParser {
|
||||
|
||||
private final SimpleDateFormat dateTimeProto20;
|
||||
private final SimpleDateFormat dateTimeProto22;
|
||||
private final SimpleDateFormat dateTimeProto23;
|
||||
private final SimpleDateFormat dateTimeProto24;
|
||||
|
||||
UtilDateTimeParser() {
|
||||
dateTimeProto20 = init("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
dateTimeProto22 = init("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
|
||||
dateTimeProto23 = init("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");
|
||||
dateTimeProto24 = init("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
||||
}
|
||||
|
||||
private SimpleDateFormat init(String dateTimeFormat) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateTimeFormat);
|
||||
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
return sdf;
|
||||
}
|
||||
|
||||
private SimpleDateFormat dtFormat(int length) {
|
||||
switch (length) {
|
||||
case 24:
|
||||
return (SimpleDateFormat) dateTimeProto24.clone();
|
||||
case 23:
|
||||
return (SimpleDateFormat) dateTimeProto23.clone();
|
||||
case 22:
|
||||
return (SimpleDateFormat) dateTimeProto22.clone();
|
||||
case 20:
|
||||
return (SimpleDateFormat) dateTimeProto20.clone();
|
||||
default:
|
||||
return (SimpleDateFormat) dateTimeProto24.clone();
|
||||
}
|
||||
}
|
||||
private static final DateTimeFormatter ISO_MILLIS = new DateTimeFormatterBuilder()
|
||||
.parseCaseInsensitive()
|
||||
.appendInstant(3)
|
||||
.toFormatter();
|
||||
|
||||
public Timestamp parse(String jsonDateTime) {
|
||||
try {
|
||||
java.util.Date d = dtFormat(jsonDateTime.length()).parse(jsonDateTime);
|
||||
return new Timestamp(d.getTime());
|
||||
} catch (ParseException e) {
|
||||
return Timestamp.from(Instant.parse(jsonDateTime));
|
||||
} catch (DateTimeParseException e) {
|
||||
throw new RuntimeException("Error parsing Datetime[" + jsonDateTime + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
public String format(Date value) {
|
||||
// always format with millisecond precision
|
||||
return dtFormat(24).format(value);
|
||||
return ISO_MILLIS.format(value.toInstant());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user