scenario
For some devices with high time accuracy, such as cameras, the time may be accurate to nanoseconds, and the createTime field will be added to the JSON string data
2021-09-28T09:21:08.710123123+08:00 Fails to convert to Date/LocalDateTime. The fastJson exception is as follows:
Exception in thread "main" com.alibaba.fastjson.JSONException: For input string: "The 2021-09-28 T09:21:08. 710123123 + 08:00"... Under Caused by: Java. Lang. A NumberFormatException: For input string: "the 2021-09-28 T09:21:08. 710123123 + 08:00"Copy the code
When the time accuracy reaches 9 bits, the last nine tails will be discarded in general scenarios, that is, the accuracy will be reduced, and the common ‘YYYY-MM-DD HH: MM: SS’ will be changed.
Time format.
OffsetDateTime type
Java8 with offset, support UTC/Greenwich, accuracy to nanosecond (ns).
Private OffsetDateTime createTime; // dm8 -datetime supports OffsetDateTime insertionCopy the code
OffsetDateTime specially receives data in 2021-09-28T9:21:08.710123123 +08:00 format, fastJSON parsing is normal after testing,
In Damon database test, normal insert, but the accuracy OffsetDateTime 9 changed to the database default accuracy 6, query normal.
Recommended scenario: This field has little interaction with other time format types. Otherwise, you need to consider the time type conversion cost
Regular expressions (recommended)
Yyyy-mm-dd HH: MM: SS format, which reduces accuracy, uses regular expressions to cut and replace the accuracy directly from the JSON string.
String jsonStr = "2021-09-28 T09:21:08. 710123123 + 08:00"; 710123123 String regex = "(\.) (\d{9})"; System.out.println(jsonStr.replaceFirst(regex,"")); // 2021-09-28T09:21:08+08:00Copy the code
String (not recommended)
JsonString -> OffsetDateTime(String) format to Date/LocalDateTime String format -> Date
Almost everywhere, garbage code…..
Know how to use OffsetDateTime…..
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private String createTime;
public void setCreateTime(String createTime) {
if (StrUtil.isBlank(createTime)) {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.createTime = simpleDateFormat.format(date);
return;
}
OffsetDateTime odt = OffsetDateTime.parse(createTime);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
this.createTime = odt.format(dtf);
}
Copy the code
parsing
public static void main(String[] args) { // example String (of ISO format) String input = "The 2021-09-28 T09:21:08. 710123123 + 08:00"; // parse it (using a standard format implicitly) OffsetDateTime odt = OffsetDateTime.parse(input); // print the result System.out.println(odt); 710123123+08:00 // If you want a different output, define a formatter DateTimeFormatter dtf = DateTimeFormatter.ofPattern( // use a desired pattern "yyyy-MM-dd HH:mm:ss", // and a desired locale (important for names) Locale.ROOT); // print that System.out.println(odt.format(dtf)); / / the 2021-09-28 09:21:08}Copy the code
\