Add CalendarUtils.toLocalDateTime(Calendar)

Add CalendarUtils.toLocalDateTime()
This commit is contained in:
Gary Gregory 2024-08-21 11:38:20 -04:00
parent df4be6cd6e
commit 328f2aedfb
3 changed files with 46 additions and 1 deletions

View File

@ -56,6 +56,8 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Make RandomUtils.insecure() public.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add RandomUtils.secureStrong().</action>
<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>
<!-- 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

@ -17,6 +17,7 @@
package org.apache.commons.lang3.time;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Locale;
import java.util.Locale.Category;
@ -59,6 +60,17 @@ public class CalendarUtils {
return new CalendarUtils(Calendar.getInstance(locale), locale);
}
/**
* Converts a Calendar to a LocalDateTime.
*
* @param calendar the Calendar to convert.
* @return a LocalDateTime.
* @since 3.17.0
*/
public static LocalDateTime toLocalDateTime(final Calendar calendar) {
return LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId());
}
private final Calendar calendar;
private final Locale locale;
@ -71,7 +83,6 @@ public class CalendarUtils {
public CalendarUtils(final Calendar calendar) {
this(calendar, Locale.getDefault());
}
/**
* Creates an instance for the given Calendar.
*
@ -82,6 +93,7 @@ public class CalendarUtils {
this.calendar = Objects.requireNonNull(calendar, "calendar");
this.locale = Objects.requireNonNull(locale, "locale");
}
/**
* Gets the current day of month.
*
@ -151,4 +163,14 @@ public class CalendarUtils {
public int getYear() {
return calendar.get(Calendar.YEAR);
}
/**
* Converts this instance to a {@link LocalDateTime}.
*
* @return a LocalDateTime.
* @since 3.17.0
*/
public LocalDateTime toLocalDateTime() {
return toLocalDateTime(calendar);
}
}

View File

@ -19,14 +19,23 @@ package org.apache.commons.lang3.time;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
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;
public class CalendarUtilsTest extends AbstractLangTest {
private static final String TIME_ZONE_GET_AVAILABLE_IDS = "java.util.TimeZone#getAvailableIDs()";
@Test
public void testGetDayOfMonth() {
assertEquals(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), CalendarUtils.getInstance().getDayOfMonth());
@ -83,4 +92,16 @@ public class CalendarUtilsTest extends AbstractLangTest {
assertEquals(Calendar.getInstance().get(Calendar.YEAR), CalendarUtils.INSTANCE.getYear());
}
@ParameterizedTest
@MethodSource(TIME_ZONE_GET_AVAILABLE_IDS)
public void testToLocalDateTime(final String id) {
final TimeZone timeZone = TimeZone.getTimeZone(id);
final ZoneId zoneId = timeZone.toZoneId();
final Calendar calendar = new GregorianCalendar(timeZone);
calendar.setTimeInMillis(0);
assertEquals(LocalDateTime.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toLocalDateTime());
final ZonedDateTime zdt1 = ZonedDateTime.of(1, 2, 3, 4, 5, 6, 0, zoneId);
calendar.setTimeInMillis(zdt1.toInstant().toEpochMilli());
assertEquals(LocalDateTime.ofInstant(zdt1.toInstant(), calendar.getTimeZone().toZoneId()), new CalendarUtils(calendar).toLocalDateTime());
}
}