Skip to content

Commit 0ed3f34

Browse files
committed
1 修改LocalDateTime类型适配器
1 parent 08827fd commit 0ed3f34

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

common/src/main/java/pro/tools/data/text/json/typeadapter/LocalDateTimeTypeAdapter.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,18 @@
99

1010
public class LocalDateTimeTypeAdapter extends ABasicTypeAdapter<LocalDateTime> {
1111

12-
public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH.mm.ss.SSSSSS");
12+
public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH.mm.ss.SSS");
1313

1414
@Override
1515
public LocalDateTime reading(JsonReader jsonReader) throws IOException {
16-
LocalDateTime dateTime = null;
17-
jsonReader.beginObject();
18-
jsonReader.nextName();
19-
String value = jsonReader.nextString().replace(":", ".");
20-
String dateStr = value.substring(0, value.lastIndexOf(46));
21-
String incStr = value.substring(value.lastIndexOf(46) + 1);
22-
String micro = String.format("%06d", Integer.valueOf(incStr));
23-
String dvalue = dateStr + "." + micro;
24-
dateTime = LocalDateTime.parse(dvalue, DATE_TIME_FORMATTER);
25-
jsonReader.endObject();
26-
return dateTime;
16+
LocalDateTime localDateTime;
17+
String value = jsonReader.nextString();
18+
localDateTime = LocalDateTime.parse(value, DATE_TIME_FORMATTER);
19+
return localDateTime;
2720
}
2821

2922
@Override
3023
public void writing(JsonWriter jsonWriter, LocalDateTime value) throws IOException {
31-
jsonWriter.beginObject().name("$timestamp").value(value.format(DATE_TIME_FORMATTER)).endObject();
24+
jsonWriter.jsonValue(value.format(DATE_TIME_FORMATTER));
3225
}
3326
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package time;
2+
3+
import org.junit.Test;
4+
import pro.tools.data.text.ToolJson;
5+
6+
import java.time.LocalDateTime;
7+
8+
/**
9+
* @author SeanDragon
10+
* <p>
11+
* Create By 2017-10-26 19:38
12+
*/
13+
public class TestLocalDateTime {
14+
@Test
15+
public void test1() {
16+
LocalDateTime localDateTime = LocalDateTime.now();
17+
String x = ToolJson.anyToJson(localDateTime);
18+
System.out.println(x);
19+
LocalDateTime localDateTime1 = ToolJson.jsonToAny(x, LocalDateTime.class);
20+
System.out.println(localDateTime1);
21+
}
22+
}

0 commit comments

Comments
 (0)