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 666ad1365 Add CalendarUtils.toZonedDateTime(Calendar)
666ad1365 is described below

commit 666ad13656c690b310fc2c6487dd60b6ed72a5ed
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Wed Aug 21 12:16:50 2024 -0400

    Add CalendarUtils.toZonedDateTime(Calendar)
    
    Add CalendarUtils.toZonedDateTime()
---
 src/changes/changes.xml                            |  2 ++
 .../apache/commons/lang3/time/CalendarUtils.java   | 30 +++++++++++++++++++++-
 .../commons/lang3/time/CalendarUtilsTest.java      | 13 ++++++++++
 3 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4bd243ba4..04a2f9f33 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -58,6 +58,8 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add RandomStringUtils.secureStrong().</action>
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add CalendarUtils.toLocalDateTime(Calendar).</action>
     <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>
     <!-- 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 3582c4700..6e7c1787c 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,8 @@
 package org.apache.commons.lang3.time;
 
 import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
 import java.util.Calendar;
 import java.util.Locale;
 import java.util.Locale.Category;
@@ -68,7 +70,22 @@ public class CalendarUtils {
      * @since 3.17.0
      */
     public static LocalDateTime toLocalDateTime(final Calendar calendar) {
-        return LocalDateTime.ofInstant(calendar.toInstant(), 
calendar.getTimeZone().toZoneId());
+        return LocalDateTime.ofInstant(calendar.toInstant(), 
toZoneId(calendar));
+    }
+
+    /**
+     * Converts a Calendar to a ZonedDateTime.
+     *
+     * @param calendar the Calendar to convert.
+     * @return a ZonedDateTime.
+     * @since 3.17.0
+     */
+    public static ZonedDateTime toZonedDateTime(final Calendar calendar) {
+        return ZonedDateTime.ofInstant(calendar.toInstant(), 
toZoneId(calendar));
+    }
+
+    private static ZoneId toZoneId(final Calendar calendar) {
+        return calendar.getTimeZone().toZoneId();
     }
 
     private final Calendar calendar;
@@ -173,4 +190,15 @@ public class CalendarUtils {
     public LocalDateTime toLocalDateTime() {
         return toLocalDateTime(calendar);
     }
+
+    /**
+     * Converts this instance to a {@link ZonedDateTime}.
+     *
+     * @return a ZonedDateTime.
+     * @since 3.17.0
+     */
+    public ZonedDateTime toZonedDateTime() {
+        return toZonedDateTime(calendar);
+    }
+
 }
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 6c0af2031..b38603558 100644
--- a/src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java
@@ -104,4 +104,17 @@ public class CalendarUtilsTest extends AbstractLangTest {
         calendar.setTimeInMillis(zdt1.toInstant().toEpochMilli());
         assertEquals(LocalDateTime.ofInstant(zdt1.toInstant(), 
calendar.getTimeZone().toZoneId()), new 
CalendarUtils(calendar).toLocalDateTime());
     }
+
+    @ParameterizedTest
+    @MethodSource(TIME_ZONE_GET_AVAILABLE_IDS)
+    public void testToZonedDateTime(final String id) {
+        final TimeZone timeZone = TimeZone.getTimeZone(id);
+        final ZoneId zoneId = timeZone.toZoneId();
+        final Calendar calendar = new GregorianCalendar(timeZone);
+        calendar.setTimeInMillis(0);
+        assertEquals(ZonedDateTime.ofInstant(calendar.toInstant(), 
calendar.getTimeZone().toZoneId()), new 
CalendarUtils(calendar).toZonedDateTime());
+        final ZonedDateTime zdt1 = ZonedDateTime.of(1, 2, 3, 4, 5, 6, 0, 
zoneId);
+        calendar.setTimeInMillis(zdt1.toInstant().toEpochMilli());
+        assertEquals(ZonedDateTime.ofInstant(zdt1.toInstant(), 
calendar.getTimeZone().toZoneId()), new 
CalendarUtils(calendar).toZonedDateTime());
+    }
 }

Reply via email to