新增了获取本月最后一天,是否为本月最后一天

This commit is contained in:
quping 2022-09-28 18:07:27 +08:00
parent 42a427193b
commit ee12875bf3
2 changed files with 28 additions and 0 deletions

View File

@ -2247,6 +2247,26 @@ public class DateUtil extends CalendarUtil {
return startTime.before(realEndTime) && endTime.after(realStartTime); return startTime.before(realEndTime) && endTime.after(realStartTime);
} }
/**
* 是否为本月最后一天
* @param dateTime {@link DateTime}
* @return 是否为本月最后一天
* @since 5.8.8
*/
public static boolean isLastDayOfMonth(DateTime dateTime){
return dateTime.dayOfMonth()==getLastDayOfMonth(dateTime);
}
/**
* 获得本月的最后一天
* @param dateTime {@link DateTime}
* @return
* @since 5.8.8
*/
public static int getLastDayOfMonth(DateTime dateTime){
return Month.getLastDay(dateTime.month(),dateTime.isLeapYear());
}
// ------------------------------------------------------------------------ Private method start // ------------------------------------------------------------------------ Private method start
/** /**

View File

@ -1094,4 +1094,12 @@ public class DateUtilTest {
DateUtil.parse(sourceStr)); DateUtil.parse(sourceStr));
Assert.assertTrue(between); Assert.assertTrue(between);
} }
@Test
public void isLastDayTest(){
DateTime dateTime = DateUtil.parse("2022-09-30");
int dayOfMonth = DateUtil.getLastDayOfMonth(dateTime);
Assert.assertEquals(dayOfMonth,dateTime.dayOfMonth());
Assert.assertTrue("not is last day of this month !!",DateUtil.isLastDayOfMonth(dateTime));
}
} }