Skip to content

Commit

Permalink
Add CalendarUtils.toLocalDate() #725
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Nov 5, 2024
1 parent fa668ef commit 80e6a67
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/apache/commons/lang3/time/CalendarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -193,6 +194,16 @@ public int getYear() {
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}.
*
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/apache/commons/lang3/time/CalendarUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

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;
Expand All @@ -32,6 +34,7 @@
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 {

Expand Down Expand Up @@ -91,6 +94,20 @@ public void testGetYear() {
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) {
Expand Down

0 comments on commit 80e6a67

Please sign in to comment.