File tree Expand file tree Collapse file tree
main/java/pro/tools/data/text/json/typeadapter Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99
1010public 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments