diff --git a/snack3/src/main/java/org/noear/snack/from/ObjectFromer.java b/snack3/src/main/java/org/noear/snack/from/ObjectFromer.java index 3014d50b2aa221242cc1ff2abad9acc63547a5b2..73dfa7e14487964412c536f617e3b713869a58f6 100644 --- a/snack3/src/main/java/org/noear/snack/from/ObjectFromer.java +++ b/snack3/src/main/java/org/noear/snack/from/ObjectFromer.java @@ -18,10 +18,7 @@ import java.sql.Clob; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; -import java.time.Instant; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; +import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*; @@ -79,6 +76,8 @@ public class ObjectFromer implements Fromer { rst.val().setString(((UUID) source).toString()); } else if (source instanceof Date) { rst.val().setDate((Date) source); + } else if (source instanceof ZonedDateTime) { + rst.val().setDate(Date.from(((ZonedDateTime) source).toInstant())); } else if (source instanceof LocalDateTime) { Instant instant = ((LocalDateTime) source).atZone(DEFAULTS.DEF_TIME_ZONE.toZoneId()).toInstant(); rst.val().setDate(new Date((instant.getEpochSecond() * 1000) + (instant.getNano() / 1000_000))); diff --git a/snack3/src/main/java/org/noear/snack/to/ObjectToer.java b/snack3/src/main/java/org/noear/snack/to/ObjectToer.java index 4ee81e0e0fc8945377b2d5945e28ca7d3c11e300..d519528d2c943639bae00c0f9182d2a8b78d4117 100644 --- a/snack3/src/main/java/org/noear/snack/to/ObjectToer.java +++ b/snack3/src/main/java/org/noear/snack/to/ObjectToer.java @@ -21,6 +21,7 @@ import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.ZonedDateTime; import java.util.*; import java.util.concurrent.atomic.DoubleAdder; import java.util.concurrent.atomic.LongAdder; @@ -244,6 +245,13 @@ public class ObjectToer implements Toer { return new java.sql.Time(v.getLong()); } else if (is(Date.class, clz)) { return v.getDate(); + }else if (is(ZonedDateTime.class, clz)) { + Date date = v.getDate(); + if (null == date) { + return null; + } else { + return ZonedDateTime.ofInstant(date.toInstant(), DEFAULTS.DEF_TIME_ZONE.toZoneId()); + } } else if (is(LocalDateTime.class, clz)) { Date date = v.getDate(); if (date == null) { diff --git a/snack3_demo/src/test/java/_models/DateModel2.java b/snack3_demo/src/test/java/_models/DateModel2.java index 705e1c443d1c37a1fd84af1dedac3bb880636877..6cc656143138ec633f8c17fa2f7f7b48c504bec4 100644 --- a/snack3_demo/src/test/java/_models/DateModel2.java +++ b/snack3_demo/src/test/java/_models/DateModel2.java @@ -3,6 +3,7 @@ package _models; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.ZonedDateTime; import java.util.Date; /** @@ -18,4 +19,5 @@ public class DateModel2 { public Date date7; public Date date8; public LocalTime date9; + public ZonedDateTime date10; } diff --git a/snack3_demo/src/test/java/_models/ZonedDateTimeModel.java b/snack3_demo/src/test/java/_models/ZonedDateTimeModel.java new file mode 100644 index 0000000000000000000000000000000000000000..b7f8748d5107998ff596a6972e42ea301a8a91cf --- /dev/null +++ b/snack3_demo/src/test/java/_models/ZonedDateTimeModel.java @@ -0,0 +1,7 @@ +package _models; + +import java.time.ZonedDateTime; + +public class ZonedDateTimeModel { + public ZonedDateTime date; +} diff --git a/snack3_demo/src/test/java/features/DateTest.java b/snack3_demo/src/test/java/features/DateTest.java index 984ed750ed44c193d76d1a5841d2d9470e20e3bf..28d62639d53e8a6fcf23d8f73ec82d158d923801 100644 --- a/snack3_demo/src/test/java/features/DateTest.java +++ b/snack3_demo/src/test/java/features/DateTest.java @@ -32,7 +32,7 @@ public class DateTest { @Test public void test3() { - String json = "{date1:'2021-06-13T20:54:51.566Z', date2:'2021-06-13T20:54:51', date3:'2021-06-13 20:54:51', date4:'20210613205451566+0800', date5:'2021-06-13', date6:'2021-06-13T20:54:51.566+08:00', date7:'2021-06-13 20:54:51,566', date8:'2021-06-13 20:54:51.566', date9:'20:54:51'}"; + String json = "{date1:'2021-06-13T20:54:51.566Z', date2:'2021-06-13T20:54:51', date3:'2021-06-13 20:54:51', date4:'20210613205451566+0800', date5:'2021-06-13', date6:'2021-06-13T20:54:51.566+08:00', date7:'2021-06-13 20:54:51,566', date8:'2021-06-13 20:54:51.566', date9:'20:54:51', date10:'2021-06-13T20:54:51.566+08:00'}"; DateModel dateModel0 = ONode.deserialize(json, DateModel.class); DateModel2 dateModel = ONode.deserialize(json, DateModel2.class); @@ -52,6 +52,7 @@ public class DateTest { assert dateModel.date7.equals(dateModel2.date7); assert dateModel.date8.equals(dateModel2.date8); assert dateModel.date9.equals(dateModel2.date9); + assert dateModel.date10.equals(dateModel2.date10); } @Test diff --git a/snack3_demo/src/test/java/features/ZonedDateTimeTest.java b/snack3_demo/src/test/java/features/ZonedDateTimeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..ad53a68aa939020c13378a1c5f45d6b64099fe07 --- /dev/null +++ b/snack3_demo/src/test/java/features/ZonedDateTimeTest.java @@ -0,0 +1,46 @@ +package features; + +import _models.ZonedDateTimeModel; +import org.junit.Test; +import org.noear.snack.ONode; +import org.noear.snack.core.Context; +import org.noear.snack.core.Options; +import org.noear.snack.from.ObjectFromer; + +import java.time.ZoneId; +import java.time.ZonedDateTime; + +/** + * ZoneDateTime序列化测试 + * @author hans + */ +public class ZonedDateTimeTest { + + + /** + * 反序列化测试 + */ + @Test + public void deserialize() { + String poc = "{\"date\":\"2024-01-12T10:30:00.000+03:00\"}"; + ONode oNode = ONode.loadStr(poc); + //解析 + ZonedDateTimeModel model = oNode.toObject(ZonedDateTimeModel.class); + ZonedDateTime date = model.date; + ZonedDateTime zonedDateTime = date.withZoneSameInstant(ZoneId.of("+03:00")); + assert date.toInstant().equals(zonedDateTime.toInstant()); + } + + /** + * 序列化测试 + */ + @Test + public void serialize() { + ZonedDateTimeModel data = new ZonedDateTimeModel(); + data.date=ZonedDateTime.now(); + ObjectFromer objectFromer = new ObjectFromer(); + Context context = new Context(Options.def(), data); + objectFromer.handle(context); + assert context.source == data; + } +} \ No newline at end of file