From bfa3c06361fa7bfcf35f8b3136f48a46c2b2c241 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 21 Aug 2024 13:40:51 -0400 Subject: [PATCH] Add CalendarUtils.toOffsetDateTime(Calendar) Add CalendarUtils.toOffsetDateTime() --- src/changes/changes.xml | 2 ++ .../commons/lang3/time/CalendarUtils.java | 22 +++++++++++++++++++ .../commons/lang3/time/CalendarUtilsTest.java | 14 ++++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 04a2f9f33..04b33b575 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -60,6 +60,8 @@ The type attribute can be add,update,fix,remove. Add CalendarUtils.toLocalDateTime(). Add CalendarUtils.toZonedDateTime(Calendar). Add CalendarUtils.toZonedDateTime(). + Add CalendarUtils.toOffsetDateTime(Calendar). + Add CalendarUtils.toOffsetDateTime(). Bump org.hamcrest:hamcrest from 2.2 to 3.0 #1255. Bump org.easymock:easymock from 5.3.0 to 5.4.0 #1256. diff --git a/src/main/java/org/apache/commons/lang3/time/CalendarUtils.java b/src/main/java/org/apache/commons/lang3/time/CalendarUtils.java index 6e7c1787c..cd6868844 100644 --- a/src/main/java/org/apache/commons/lang3/time/CalendarUtils.java +++ b/src/main/java/org/apache/commons/lang3/time/CalendarUtils.java @@ -18,6 +18,7 @@ package org.apache.commons.lang3.time; import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Calendar; @@ -73,6 +74,17 @@ public class CalendarUtils { return LocalDateTime.ofInstant(calendar.toInstant(), toZoneId(calendar)); } + /** + * Converts a Calendar to a OffsetDateTime. + * + * @param calendar the Calendar to convert. + * @return a OffsetDateTime. + * @since 3.17.0 + */ + public static OffsetDateTime toOffsetDateTime(final Calendar calendar) { + return OffsetDateTime.ofInstant(calendar.toInstant(), toZoneId(calendar)); + } + /** * Converts a Calendar to a ZonedDateTime. * @@ -191,6 +203,16 @@ public class CalendarUtils { return toLocalDateTime(calendar); } + /** + * Converts this instance to a {@link OffsetDateTime}. + * + * @return a OffsetDateTime. + * @since 3.17.0 + */ + public OffsetDateTime toOffsetDateTime() { + return toOffsetDateTime(calendar); + } + /** * Converts this instance to a {@link ZonedDateTime}. * diff --git a/src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java b/src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java index b38603558..7e9a5c8d2 100644 --- a/src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java @@ -20,6 +20,7 @@ package org.apache.commons.lang3.time; import static org.junit.jupiter.api.Assertions.assertEquals; import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Calendar; @@ -105,6 +106,19 @@ public class CalendarUtilsTest extends AbstractLangTest { assertEquals(LocalDateTime.ofInstant(zdt1.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toLocalDateTime()); } + @ParameterizedTest + @MethodSource(TIME_ZONE_GET_AVAILABLE_IDS) + public void testToOffsetDateTime(final String id) { + final TimeZone timeZone = TimeZone.getTimeZone(id); + final ZoneId zoneId = timeZone.toZoneId(); + final Calendar calendar = new GregorianCalendar(timeZone); + calendar.setTimeInMillis(0); + assertEquals(OffsetDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toOffsetDateTime()); + final ZonedDateTime zdt1 = ZonedDateTime.of(1, 2, 3, 4, 5, 6, 0, zoneId); + calendar.setTimeInMillis(zdt1.toInstant().toEpochMilli()); + assertEquals(OffsetDateTime.ofInstant(zdt1.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toOffsetDateTime()); + } + @ParameterizedTest @MethodSource(TIME_ZONE_GET_AVAILABLE_IDS) public void testToZonedDateTime(final String id) {