diff --git a/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java b/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java index 22e46472..d65422a3 100644 --- a/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/CalendarConversions.java @@ -61,7 +61,7 @@ static java.sql.Date toSqlDate(Object from, Converter converter) { } static Timestamp toTimestamp(Object from, Converter converter) { - return new Timestamp(((Calendar) from).getTime().getTime()); + return new Timestamp(((Calendar) from).getTimeInMillis()); } static AtomicLong toAtomicLong(Object from, Converter converter) { diff --git a/src/main/java/com/cedarsoftware/util/convert/Converter.java b/src/main/java/com/cedarsoftware/util/convert/Converter.java index 52a1625d..35abfa10 100644 --- a/src/main/java/com/cedarsoftware/util/convert/Converter.java +++ b/src/main/java/com/cedarsoftware/util/convert/Converter.java @@ -511,7 +511,7 @@ private static void buildFactoryConversions() { CONVERSION_DB.put(pair(AtomicLong.class, Calendar.class), NumberConversions::toCalendar); CONVERSION_DB.put(pair(Date.class, Calendar.class), DateConversions::toCalendar); CONVERSION_DB.put(pair(java.sql.Date.class, Calendar.class), DateConversions::toCalendar); - CONVERSION_DB.put(pair(Timestamp.class, Calendar.class), DateConversions::toCalendar); + CONVERSION_DB.put(pair(Timestamp.class, Calendar.class), TimestampConversions::toCalendar); CONVERSION_DB.put(pair(Instant.class, Calendar.class), InstantConversions::toCalendar); CONVERSION_DB.put(pair(LocalTime.class, Calendar.class), LocalTimeConversions::toCalendar); CONVERSION_DB.put(pair(LocalDate.class, Calendar.class), LocalDateConversions::toCalendar); diff --git a/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java b/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java index f690d866..ff2ec670 100644 --- a/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/LocalTimeConversions.java @@ -98,7 +98,6 @@ static Calendar toCalendar(Object from, Converter converter) { cal.set(Calendar.MINUTE, localTime.getMinute()); cal.set(Calendar.SECOND, localTime.getSecond()); cal.set(Calendar.MILLISECOND, localTime.getNano() / 1_000_000); // Convert nanoseconds to milliseconds - cal.getTime(); // compute fields return cal; } } diff --git a/src/main/java/com/cedarsoftware/util/convert/MapConversions.java b/src/main/java/com/cedarsoftware/util/convert/MapConversions.java index 05c1a7be..4e23fcd0 100644 --- a/src/main/java/com/cedarsoftware/util/convert/MapConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/MapConversions.java @@ -252,7 +252,6 @@ else if (map.containsKey(YEAR)) { cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, second); cal.set(Calendar.MILLISECOND, ms); - cal.getTime(); return cal; } else { return fromValueForMultiKey(map, converter, Calendar.class, CALENDAR_PARAMS); diff --git a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java index 82ef59e2..432b82eb 100644 --- a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java @@ -366,23 +366,10 @@ static Calendar toCalendar(Object from, Converter converter) { return null; } ZonedDateTime zdt = DateUtilities.parseDate(calStr, converter.getOptions().getZoneId(), true); - if (zdt == null) { - return null; - } ZonedDateTime zdtUser = zdt.withZoneSameInstant(converter.getOptions().getZoneId()); - // Must copy this way. Using the GregorianCalendar.from(zdt) does not pass .equals() later. - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.YEAR, zdt.getYear()); - cal.set(Calendar.MONTH, zdt.getMonthValue() - 1); - cal.set(Calendar.DAY_OF_MONTH, zdt.getDayOfMonth()); - cal.set(Calendar.HOUR_OF_DAY, zdt.getHour()); - cal.set(Calendar.MINUTE, zdt.getMinute()); - cal.set(Calendar.SECOND, zdt.getSecond()); - cal.set(Calendar.MILLISECOND, zdt.getNano() / 1_000_000); - cal.setTimeZone(TimeZone.getTimeZone(zdtUser.getZone())); - cal.getTime(); - + Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(zdtUser.getZone())); + cal.setTimeInMillis(zdtUser.toInstant().toEpochMilli()); return cal; } diff --git a/src/main/java/com/cedarsoftware/util/convert/TimestampConversions.java b/src/main/java/com/cedarsoftware/util/convert/TimestampConversions.java index bbe54db9..a03bf185 100644 --- a/src/main/java/com/cedarsoftware/util/convert/TimestampConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/TimestampConversions.java @@ -8,6 +8,7 @@ import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import java.util.Calendar; /** * @author John DeRegnaucourt (jdereg@gmail.com) @@ -62,4 +63,11 @@ static OffsetDateTime toOffsetDateTime(Object from, Converter converter) { return timestamp.toInstant().atOffset(zoneOffset); } + + static Calendar toCalendar(Object from, Converter converter) { + Timestamp timestamp = (Timestamp) from; + Calendar cal = Calendar.getInstance(converter.getOptions().getTimeZone()); + cal.setTimeInMillis(timestamp.getTime()); + return cal; + } } diff --git a/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java b/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java index b82245ca..16ccb43d 100644 --- a/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java +++ b/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java @@ -190,7 +190,6 @@ private static void loadMapTests() { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.set(2024, Calendar.FEBRUARY, 5, 22, 31, 17); cal.set(Calendar.MILLISECOND, 409); - cal.getTime(); return cal; }, (Supplier>) () -> { Map map = new CompactLinkedMap<>(); @@ -421,7 +420,6 @@ private static void loadStringTests() { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.set(2024, Calendar.FEBRUARY, 5, 22, 31, 17); cal.set(Calendar.MILLISECOND, 409); - cal.getTime(); return cal; }, "2024-02-05T22:31:17.409+09:00"} }); @@ -599,7 +597,6 @@ private static void loadLocalDateTimeTests() { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.set(2024, Calendar.MARCH, 2, 22, 54, 17); cal.set(Calendar.MILLISECOND, 0); - cal.getTime(); return cal; }, LocalDateTime.of(2024, Month.MARCH, 2, 22, 54, 17), true } }); @@ -675,7 +672,6 @@ private static void loadLocalTimeTests() { cal.set(Calendar.MINUTE, 47); cal.set(Calendar.SECOND, 55); cal.set(Calendar.MILLISECOND, 0); - cal.getTime(); return cal; }, LocalTime.of(22, 47, 55), true } }); @@ -775,6 +771,13 @@ private static void loadTimestampTests() { {new BigDecimal(".999999999"), Timestamp.from(Instant.parse("1970-01-01T00:00:00.999999999Z")), true}, {new BigDecimal("1"), Timestamp.from(Instant.parse("1970-01-01T00:00:01Z")), true}, }); + TEST_DB.put(pair(Calendar.class, Timestamp.class), new Object[][] { + {(Supplier) () -> { + Calendar cal = Calendar.getInstance(TOKYO_TZ); + cal.setTimeInMillis(now); + return cal; + }, new Timestamp(now), true}, + }); TEST_DB.put(pair(Duration.class, Timestamp.class), new Object[][]{ {Duration.ofSeconds(-62167219200L), Timestamp.from(Instant.parse("0000-01-01T00:00:00Z")), true}, {Duration.ofSeconds(-62167219200L, 1), Timestamp.from(Instant.parse("0000-01-01T00:00:00.000000001Z")), true}, @@ -1086,7 +1089,6 @@ private static void loadSqlDateTests() { {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(now); - cal.getTime(); return cal; }, new java.sql.Date(now), true} }); @@ -1113,7 +1115,6 @@ private static void loadDateTests() { {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(now); - cal.getTime(); return cal; }, new Date(now), true } }); @@ -1130,12 +1131,10 @@ private static void loadCalendarTests() { {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(now); - cal.getTime(); return cal; }, (Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(now); - cal.getTime(); return cal; } } }); @@ -1143,19 +1142,16 @@ private static void loadCalendarTests() { {new AtomicLong(-1), (Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(-1); - cal.getTime(); return cal; }, true}, {new AtomicLong(0), (Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(0); - cal.getTime(); return cal; }, true}, {new AtomicLong(1), (Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(1); - cal.getTime(); return cal; }, true}, }); @@ -1163,19 +1159,16 @@ private static void loadCalendarTests() { {new BigDecimal(-1), (Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(-1000); - cal.getTime(); return cal; }, true}, {new BigDecimal("-0.001"), (Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(-1); - cal.getTime(); return cal; }, true}, {BigDecimal.ZERO, (Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(0); - cal.getTime(); return cal; }, true}, {new BigDecimal("0.001"), (Supplier) () -> { @@ -1210,6 +1203,19 @@ private static void loadCalendarTests() { cal.set(Calendar.MILLISECOND, 409); return cal; }}, + {(Supplier>) () -> { + Map map = new CompactLinkedMap<>(); + Calendar cal = Calendar.getInstance(TOKYO_TZ); + cal.set(2024, Calendar.FEBRUARY, 5, 22, 31, 17); + cal.set(Calendar.MILLISECOND, 409); + map.put(VALUE, cal); + return map; + }, (Supplier) () -> { + Calendar cal = Calendar.getInstance(TOKYO_TZ); + cal.set(2024, Calendar.FEBRUARY, 5, 22, 31, 17); + cal.set(Calendar.MILLISECOND, 409); + return cal; + }}, }); } @@ -1246,7 +1252,6 @@ private static void loadInstantTests() { {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(now); - cal.getTime(); return cal; }, Instant.ofEpochMilli(now), true } }); @@ -1467,19 +1472,16 @@ private static void loadBigIntegerTests() { {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(-1); - cal.getTime(); return cal; }, BigInteger.valueOf(-1000000), true}, {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(0); - cal.getTime(); return cal; }, BigInteger.ZERO, true}, {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(1); - cal.getTime(); return cal; }, BigInteger.valueOf(1000000), true}, }); @@ -1866,31 +1868,26 @@ private static void loadDoubleTests() { {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(-1000); - cal.getTime(); return cal; }, -1.0, true}, {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(-1); - cal.getTime(); return cal; }, -0.001, true}, {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(0); - cal.getTime(); return cal; }, 0d, true}, {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(1); - cal.getTime(); return cal; }, 0.001d, true}, {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(1000); - cal.getTime(); return cal; }, 1.0, true}, }); @@ -2334,13 +2331,11 @@ private static void loadLongTests() { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.set(2024, Calendar.FEBRUARY, 12, 11, 38, 0); cal.set(Calendar.MILLISECOND, 0); - cal.getTime(); return cal; }, 1707705480000L}, {(Supplier) () -> { Calendar cal = Calendar.getInstance(TOKYO_TZ); cal.setTimeInMillis(now); // Calendar maintains time to millisecond resolution - cal.getTime(); return cal; }, now} }); diff --git a/src/test/java/com/cedarsoftware/util/convert/ConverterTest.java b/src/test/java/com/cedarsoftware/util/convert/ConverterTest.java index e7901fb6..d7e73998 100644 --- a/src/test/java/com/cedarsoftware/util/convert/ConverterTest.java +++ b/src/test/java/com/cedarsoftware/util/convert/ConverterTest.java @@ -2326,7 +2326,6 @@ void toCalendar(Object source) { Long epochMilli = 1687622249729L; - System.out.println("source.getClass().getName() = " + source.getClass().getName()); Calendar calendar = this.converter.convert(source, Calendar.class); assertEquals(calendar.getTime().getTime(), epochMilli);