Add CalendarUtils.toZonedDateTime(Calendar)

Add CalendarUtils.toZonedDateTime()
This commit is contained in:
Gary Gregory 2024-08-21 12:16:50 -04:00
parent 328f2aedfb
commit 666ad13656
3 changed files with 44 additions and 1 deletions

View File

@ -58,6 +58,8 @@ The <action> type attribute can be add,update,fix,remove.
<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>
<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>
<!-- 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,8 @@
package org.apache.commons.lang3.time;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Locale;
import java.util.Locale.Category;
@ -68,7 +70,22 @@ public class CalendarUtils {
* @since 3.17.0
*/
public static LocalDateTime toLocalDateTime(final Calendar calendar) {
return LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId());
return LocalDateTime.ofInstant(calendar.toInstant(), toZoneId(calendar));
}
/**
* Converts a Calendar to a ZonedDateTime.
*
* @param calendar the Calendar to convert.
* @return a ZonedDateTime.
* @since 3.17.0
*/
public static ZonedDateTime toZonedDateTime(final Calendar calendar) {
return ZonedDateTime.ofInstant(calendar.toInstant(), toZoneId(calendar));
}
private static ZoneId toZoneId(final Calendar calendar) {
return calendar.getTimeZone().toZoneId();
}
private final Calendar calendar;
@ -173,4 +190,15 @@ public class CalendarUtils {
public LocalDateTime toLocalDateTime() {
return toLocalDateTime(calendar);
}
/**
* Converts this instance to a {@link ZonedDateTime}.
*
* @return a ZonedDateTime.
* @since 3.17.0
*/
public ZonedDateTime toZonedDateTime() {
return toZonedDateTime(calendar);
}
}

View File

@ -104,4 +104,17 @@ public class CalendarUtilsTest extends AbstractLangTest {
calendar.setTimeInMillis(zdt1.toInstant().toEpochMilli());
assertEquals(LocalDateTime.ofInstant(zdt1.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toLocalDateTime());
}
@ParameterizedTest
@MethodSource(TIME_ZONE_GET_AVAILABLE_IDS)
public void testToZonedDateTime(final String id) {
final TimeZone timeZone = TimeZone.getTimeZone(id);
final ZoneId zoneId = timeZone.toZoneId();
final Calendar calendar = new GregorianCalendar(timeZone);
calendar.setTimeInMillis(0);
assertEquals(ZonedDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toZonedDateTime());
final ZonedDateTime zdt1 = ZonedDateTime.of(1, 2, 3, 4, 5, 6, 0, zoneId);
calendar.setTimeInMillis(zdt1.toInstant().toEpochMilli());
assertEquals(ZonedDateTime.ofInstant(zdt1.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toZonedDateTime());
}
}