This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new bfa3c0636 Add CalendarUtils.toOffsetDateTime(Calendar)
bfa3c0636 is described below
commit bfa3c06361fa7bfcf35f8b3136f48a46c2b2c241
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Aug 21 13:40:51 2024 -0400
Add CalendarUtils.toOffsetDateTime(Calendar)
Add CalendarUtils.toOffsetDateTime()
---
src/changes/changes.xml | 2 ++
.../apache/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 <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add CalendarUtils.toLocalDateTime().</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add CalendarUtils.toZonedDateTime(Calendar).</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add CalendarUtils.toZonedDateTime().</action>
+ <action type="add" dev="ggregory" due-to="Gary
Gregory">Add CalendarUtils.toOffsetDateTime(Calendar).</action>
+ <action type="add" dev="ggregory" due-to="Gary
Gregory">Add CalendarUtils.toOffsetDateTime().</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary
Gregory, Dependabot">Bump org.hamcrest:hamcrest from 2.2 to 3.0 #1255.</action>
<action type="update" dev="ggregory" due-to="Gary
Gregory, Dependabot">Bump org.easymock:easymock from 5.3.0 to 5.4.0
#1256.</action>
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) {