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 80e6a6764 Add CalendarUtils.toLocalDate() #725
80e6a6764 is described below
commit 80e6a67649ec10d67f5a574c8e93248615c347ca
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Nov 5 06:31:03 2024 -0500
Add CalendarUtils.toLocalDate() #725
---
src/changes/changes.xml | 1 +
.../org/apache/commons/lang3/time/CalendarUtils.java | 11 +++++++++++
.../apache/commons/lang3/time/CalendarUtilsTest.java | 17 +++++++++++++++++
3 files changed, 29 insertions(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index eaaef7d13..f1566c437 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -73,6 +73,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add LongRange.toLongStream().</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add IntStrams.of(int...).</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add ArrayUtils.containsAny(int[], int...).</action>
+ <action type="add" dev="ggregory" due-to="asgh, Gary
Gregory">Add CalendarUtils.toLocalDate() #725.</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary
Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 78
#1267, #1277, #1283, #1288, #1302.</action>
<action type="update" dev="ggregory" due-to="Gary
Gregory, Dependabot">Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to
3.2.1 #1300.</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 cd6868844..e4671a3d1 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.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
@@ -193,6 +194,16 @@ public class CalendarUtils {
return calendar.get(Calendar.YEAR);
}
+ /**
+ * Converts this instance to a {@link LocalDate}.
+ *
+ * @return a LocalDateTime.
+ * @since 3.18.0
+ */
+ public LocalDate toLocalDate() {
+ return toLocalDateTime().toLocalDate();
+ }
+
/**
* Converts this instance to a {@link LocalDateTime}.
*
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 067f1b923..572602d28 100644
--- a/src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java
@@ -19,7 +19,9 @@ package org.apache.commons.lang3.time;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import java.time.LocalDate;
import java.time.LocalDateTime;
+import java.time.Month;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
@@ -32,6 +34,7 @@ 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;
+import org.junitpioneer.jupiter.DefaultTimeZone;
public class CalendarUtilsTest extends AbstractLangTest {
@@ -91,6 +94,20 @@ public class CalendarUtilsTest extends AbstractLangTest {
assertEquals(Calendar.getInstance().get(Calendar.YEAR),
CalendarUtils.INSTANCE.getYear());
}
+ /**
+ * Tests {@link CalendarUtils#toLocalDate()} from
https://github.com/apache/commons-lang/pull/725.
+ */
+ @Test
+ @DefaultTimeZone("GMT-5")
+ public void testToLocalDate() {
+ final Calendar calendar = new
GregorianCalendar(TimeZone.getTimeZone(TimeZones.GMT_ID));
+ calendar.setTimeInMillis(-27078001200000L);
+ assertEquals("1111-12-08T05:00:00Z", calendar.toInstant().toString());
+ assertEquals(LocalDate.of(1111, Month.DECEMBER, 8), new
CalendarUtils(calendar).toLocalDate());
+ calendar.setTimeInMillis(1614700215000L);
+ assertEquals(LocalDate.of(2021, Month.MARCH, 2), new
CalendarUtils(calendar).toLocalDate());
+ }
+
@ParameterizedTest
@MethodSource(TimeZonesTest.TIME_ZONE_GET_AVAILABLE_IDS)
public void testToLocalDateTime(final String timeZoneId) {