Skip to content

Commit 80e6a67

Browse files
committed
Add CalendarUtils.toLocalDate() #725
1 parent fa668ef commit 80e6a67

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ The <action> type attribute can be add,update,fix,remove.
7373
<action type="add" dev="ggregory" due-to="Gary Gregory">Add LongRange.toLongStream().</action>
7474
<action type="add" dev="ggregory" due-to="Gary Gregory">Add IntStrams.of(int...).</action>
7575
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ArrayUtils.containsAny(int[], int...).</action>
76+
<action type="add" dev="ggregory" due-to="asgh, Gary Gregory">Add CalendarUtils.toLocalDate() #725.</action>
7677
<!-- UPDATE -->
7778
<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>
7879
<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>

src/main/java/org/apache/commons/lang3/time/CalendarUtils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.commons.lang3.time;
1919

20+
import java.time.LocalDate;
2021
import java.time.LocalDateTime;
2122
import java.time.OffsetDateTime;
2223
import java.time.ZoneId;
@@ -193,6 +194,16 @@ public int getYear() {
193194
return calendar.get(Calendar.YEAR);
194195
}
195196

197+
/**
198+
* Converts this instance to a {@link LocalDate}.
199+
*
200+
* @return a LocalDateTime.
201+
* @since 3.18.0
202+
*/
203+
public LocalDate toLocalDate() {
204+
return toLocalDateTime().toLocalDate();
205+
}
206+
196207
/**
197208
* Converts this instance to a {@link LocalDateTime}.
198209
*

src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121

22+
import java.time.LocalDate;
2223
import java.time.LocalDateTime;
24+
import java.time.Month;
2325
import java.time.OffsetDateTime;
2426
import java.time.ZoneId;
2527
import java.time.ZonedDateTime;
@@ -32,6 +34,7 @@
3234
import org.junit.jupiter.api.Test;
3335
import org.junit.jupiter.params.ParameterizedTest;
3436
import org.junit.jupiter.params.provider.MethodSource;
37+
import org.junitpioneer.jupiter.DefaultTimeZone;
3538

3639
public class CalendarUtilsTest extends AbstractLangTest {
3740

@@ -91,6 +94,20 @@ public void testGetYear() {
9194
assertEquals(Calendar.getInstance().get(Calendar.YEAR), CalendarUtils.INSTANCE.getYear());
9295
}
9396

97+
/**
98+
* Tests {@link CalendarUtils#toLocalDate()} from https://github.com/apache/commons-lang/pull/725.
99+
*/
100+
@Test
101+
@DefaultTimeZone("GMT-5")
102+
public void testToLocalDate() {
103+
final Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone(TimeZones.GMT_ID));
104+
calendar.setTimeInMillis(-27078001200000L);
105+
assertEquals("1111-12-08T05:00:00Z", calendar.toInstant().toString());
106+
assertEquals(LocalDate.of(1111, Month.DECEMBER, 8), new CalendarUtils(calendar).toLocalDate());
107+
calendar.setTimeInMillis(1614700215000L);
108+
assertEquals(LocalDate.of(2021, Month.MARCH, 2), new CalendarUtils(calendar).toLocalDate());
109+
}
110+
94111
@ParameterizedTest
95112
@MethodSource(TimeZonesTest.TIME_ZONE_GET_AVAILABLE_IDS)
96113
public void testToLocalDateTime(final String timeZoneId) {

0 commit comments

Comments
 (0)