Add CalendarUtils.toOffsetDateTime(Calendar)

Add CalendarUtils.toOffsetDateTime()
This commit is contained in:
Gary Gregory 2024-08-21 13:40:51 -04:00
parent 666ad13656
commit bfa3c06361
3 changed files with 38 additions and 0 deletions

View File

@ -60,6 +60,8 @@ The <action> type attribute can be add,update,fix,remove.
<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>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CalendarUtils.toOffsetDateTime(Calendar).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CalendarUtils.toOffsetDateTime().</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>

View File

@ -18,6 +18,7 @@
package org.apache.commons.lang3.time;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
@ -73,6 +74,17 @@ public class CalendarUtils {
return LocalDateTime.ofInstant(calendar.toInstant(), toZoneId(calendar));
}
/**
* Converts a Calendar to a OffsetDateTime.
*
* @param calendar the Calendar to convert.
* @return a OffsetDateTime.
* @since 3.17.0
*/
public static OffsetDateTime toOffsetDateTime(final Calendar calendar) {
return OffsetDateTime.ofInstant(calendar.toInstant(), toZoneId(calendar));
}
/**
* Converts a Calendar to a ZonedDateTime.
*
@ -191,6 +203,16 @@ public class CalendarUtils {
return toLocalDateTime(calendar);
}
/**
* Converts this instance to a {@link OffsetDateTime}.
*
* @return a OffsetDateTime.
* @since 3.17.0
*/
public OffsetDateTime toOffsetDateTime() {
return toOffsetDateTime(calendar);
}
/**
* Converts this instance to a {@link ZonedDateTime}.
*

View File

@ -20,6 +20,7 @@ package org.apache.commons.lang3.time;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
@ -105,6 +106,19 @@ public class CalendarUtilsTest extends AbstractLangTest {
assertEquals(LocalDateTime.ofInstant(zdt1.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toLocalDateTime());
}
@ParameterizedTest
@MethodSource(TIME_ZONE_GET_AVAILABLE_IDS)
public void testToOffsetDateTime(final String id) {
final TimeZone timeZone = TimeZone.getTimeZone(id);
final ZoneId zoneId = timeZone.toZoneId();
final Calendar calendar = new GregorianCalendar(timeZone);
calendar.setTimeInMillis(0);
assertEquals(OffsetDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toOffsetDateTime());
final ZonedDateTime zdt1 = ZonedDateTime.of(1, 2, 3, 4, 5, 6, 0, zoneId);
calendar.setTimeInMillis(zdt1.toInstant().toEpochMilli());
assertEquals(OffsetDateTime.ofInstant(zdt1.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toOffsetDateTime());
}
@ParameterizedTest
@MethodSource(TIME_ZONE_GET_AVAILABLE_IDS)
public void testToZonedDateTime(final String id) {