You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/08/21 15:38:27 UTC
(commons-lang) branch master updated: Add CalendarUtils.toLocalDateTime(Calendar)
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 328f2aedf Add CalendarUtils.toLocalDateTime(Calendar)
328f2aedf is described below
commit 328f2aedfb8643c3e1e74b5aec9e3628c3cbf2e7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Aug 21 11:38:20 2024 -0400
Add CalendarUtils.toLocalDateTime(Calendar)
Add CalendarUtils.toLocalDateTime()
---
src/changes/changes.xml | 2 ++
.../apache/commons/lang3/time/CalendarUtils.java | 24 +++++++++++++++++++++-
.../commons/lang3/time/CalendarUtilsTest.java | 21 +++++++++++++++++++
3 files changed, 46 insertions(+), 1 deletion(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index bcdddf6d0..4bd243ba4 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -56,6 +56,8 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Make RandomUtils.insecure() public.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add RandomUtils.secureStrong().</action>
<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>
<!-- 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 0f7906df4..3582c4700 100644
--- a/src/main/java/org/apache/commons/lang3/time/CalendarUtils.java
+++ b/src/main/java/org/apache/commons/lang3/time/CalendarUtils.java
@@ -17,6 +17,7 @@
package org.apache.commons.lang3.time;
+import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Locale;
import java.util.Locale.Category;
@@ -59,6 +60,17 @@ public class CalendarUtils {
return new CalendarUtils(Calendar.getInstance(locale), locale);
}
+ /**
+ * Converts a Calendar to a LocalDateTime.
+ *
+ * @param calendar the Calendar to convert.
+ * @return a LocalDateTime.
+ * @since 3.17.0
+ */
+ public static LocalDateTime toLocalDateTime(final Calendar calendar) {
+ return LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId());
+ }
+
private final Calendar calendar;
private final Locale locale;
@@ -71,7 +83,6 @@ public class CalendarUtils {
public CalendarUtils(final Calendar calendar) {
this(calendar, Locale.getDefault());
}
-
/**
* Creates an instance for the given Calendar.
*
@@ -82,6 +93,7 @@ public class CalendarUtils {
this.calendar = Objects.requireNonNull(calendar, "calendar");
this.locale = Objects.requireNonNull(locale, "locale");
}
+
/**
* Gets the current day of month.
*
@@ -151,4 +163,14 @@ public class CalendarUtils {
public int getYear() {
return calendar.get(Calendar.YEAR);
}
+
+ /**
+ * Converts this instance to a {@link LocalDateTime}.
+ *
+ * @return a LocalDateTime.
+ * @since 3.17.0
+ */
+ public LocalDateTime toLocalDateTime() {
+ return toLocalDateTime(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 1faea43f6..6c0af2031 100644
--- a/src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java
@@ -19,14 +19,23 @@ package org.apache.commons.lang3.time;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
import java.util.Calendar;
+import java.util.GregorianCalendar;
import java.util.Locale;
+import java.util.TimeZone;
import org.apache.commons.lang3.AbstractLangTest;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
public class CalendarUtilsTest extends AbstractLangTest {
+ private static final String TIME_ZONE_GET_AVAILABLE_IDS = "java.util.TimeZone#getAvailableIDs()";
+
@Test
public void testGetDayOfMonth() {
assertEquals(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), CalendarUtils.getInstance().getDayOfMonth());
@@ -83,4 +92,16 @@ public class CalendarUtilsTest extends AbstractLangTest {
assertEquals(Calendar.getInstance().get(Calendar.YEAR), CalendarUtils.INSTANCE.getYear());
}
+ @ParameterizedTest
+ @MethodSource(TIME_ZONE_GET_AVAILABLE_IDS)
+ public void testToLocalDateTime(final String id) {
+ final TimeZone timeZone = TimeZone.getTimeZone(id);
+ final ZoneId zoneId = timeZone.toZoneId();
+ final Calendar calendar = new GregorianCalendar(timeZone);
+ calendar.setTimeInMillis(0);
+ assertEquals(LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toLocalDateTime());
+ final ZonedDateTime zdt1 = ZonedDateTime.of(1, 2, 3, 4, 5, 6, 0, zoneId);
+ calendar.setTimeInMillis(zdt1.toInstant().toEpochMilli());
+ assertEquals(LocalDateTime.ofInstant(zdt1.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toLocalDateTime());
+ }
}