Add CalendarUtils.toLocalDateTime(Calendar)
Add CalendarUtils.toLocalDateTime()
This commit is contained in:
parent
df4be6cd6e
commit
328f2aedfb
|
@ -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">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 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 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 -->
|
<!-- 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.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>
|
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.easymock:easymock from 5.3.0 to 5.4.0 #1256.</action>
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
package org.apache.commons.lang3.time;
|
package org.apache.commons.lang3.time;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Locale.Category;
|
import java.util.Locale.Category;
|
||||||
|
@ -59,6 +60,17 @@ public class CalendarUtils {
|
||||||
return new CalendarUtils(Calendar.getInstance(locale), locale);
|
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 Calendar calendar;
|
||||||
|
|
||||||
private final Locale locale;
|
private final Locale locale;
|
||||||
|
@ -71,7 +83,6 @@ public class CalendarUtils {
|
||||||
public CalendarUtils(final Calendar calendar) {
|
public CalendarUtils(final Calendar calendar) {
|
||||||
this(calendar, Locale.getDefault());
|
this(calendar, Locale.getDefault());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an instance for the given Calendar.
|
* Creates an instance for the given Calendar.
|
||||||
*
|
*
|
||||||
|
@ -82,6 +93,7 @@ public class CalendarUtils {
|
||||||
this.calendar = Objects.requireNonNull(calendar, "calendar");
|
this.calendar = Objects.requireNonNull(calendar, "calendar");
|
||||||
this.locale = Objects.requireNonNull(locale, "locale");
|
this.locale = Objects.requireNonNull(locale, "locale");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the current day of month.
|
* Gets the current day of month.
|
||||||
*
|
*
|
||||||
|
@ -151,4 +163,14 @@ public class CalendarUtils {
|
||||||
public int getYear() {
|
public int getYear() {
|
||||||
return calendar.get(Calendar.YEAR);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,14 +19,23 @@ package org.apache.commons.lang3.time;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
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.Calendar;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.apache.commons.lang3.AbstractLangTest;
|
import org.apache.commons.lang3.AbstractLangTest;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
public class CalendarUtilsTest extends AbstractLangTest {
|
public class CalendarUtilsTest extends AbstractLangTest {
|
||||||
|
|
||||||
|
private static final String TIME_ZONE_GET_AVAILABLE_IDS = "java.util.TimeZone#getAvailableIDs()";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetDayOfMonth() {
|
public void testGetDayOfMonth() {
|
||||||
assertEquals(Calendar.getInstance().get(Calendar.DAY_OF_MONTH), CalendarUtils.getInstance().getDayOfMonth());
|
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());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue