mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
Pre Merge pull request !1361 from Mink/v5-dev
This commit is contained in:
commit
23dd2d7534
@ -1324,6 +1324,71 @@ public class DateUtil extends CalendarUtil {
|
|||||||
public static DateTime endOfYear(Date date) {
|
public static DateTime endOfYear(Date date) {
|
||||||
return new DateTime(endOfYear(calendar(date)));
|
return new DateTime(endOfYear(calendar(date)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上一个该月日的时间(如果该月日在未来,则取去年)
|
||||||
|
*
|
||||||
|
* @param month 月份 (1-12)
|
||||||
|
* @param day 日期 (1-31)
|
||||||
|
* @return 上一个该月日的日期时间
|
||||||
|
*/
|
||||||
|
public static DateTime getLastDateOfMonthDay(int month, int day) {
|
||||||
|
DateTime now = date();
|
||||||
|
int currentYear = now.year();
|
||||||
|
|
||||||
|
boolean isLeapYear = isLeapYear(currentYear);
|
||||||
|
if (month == 2 && day == 29 && !isLeapYear) {
|
||||||
|
day = 28;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构造当年的目标日期
|
||||||
|
DateTime targetDate = parse(currentYear + "-" + month + "-" + day);
|
||||||
|
|
||||||
|
if (targetDate.isAfter(now)) {
|
||||||
|
isLeapYear = isLeapYear(currentYear + 1);
|
||||||
|
if (month == 2 && day == 29 && !isLeapYear) {
|
||||||
|
day = 28;
|
||||||
|
}
|
||||||
|
// 如果目标日期在今年还没到,则取去年
|
||||||
|
targetDate = parse((currentYear - 1) + "-" + month + "-" + day);
|
||||||
|
}
|
||||||
|
|
||||||
|
return targetDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取下一个该月日的时间(如果该月日已过去或等于今天,则取明年)
|
||||||
|
*
|
||||||
|
* @param month 月份 (1-12)
|
||||||
|
* @param day 日期 (1-31)
|
||||||
|
* @return 下一个该月日的日期时间
|
||||||
|
*/
|
||||||
|
public static DateTime getNextDateOfMonthDay(int month, int day) {
|
||||||
|
DateTime now = date();
|
||||||
|
int currentYear = now.year();
|
||||||
|
|
||||||
|
boolean isLeapYear = isLeapYear(currentYear);
|
||||||
|
if (month == 2 && day == 29 && !isLeapYear) {
|
||||||
|
day = 28;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构造当年的目标日期
|
||||||
|
DateTime targetDate = parse(currentYear + "-" + month + "-" + day);
|
||||||
|
|
||||||
|
if (targetDate.isBefore(now) || isSameDay(targetDate, now)) {
|
||||||
|
isLeapYear = isLeapYear(currentYear + 1);
|
||||||
|
if (month == 2 && day == 29 && !isLeapYear) {
|
||||||
|
day = 28;
|
||||||
|
}
|
||||||
|
// 如果目标日期在今年已经过去或等于今天,则取明年
|
||||||
|
targetDate = parse((currentYear + 1) + "-" + month + "-" + day);
|
||||||
|
}
|
||||||
|
|
||||||
|
return targetDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// --------------------------------------------------- Offset for now
|
// --------------------------------------------------- Offset for now
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1228,4 +1228,16 @@ public class DateUtilTest {
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getLastDateOfMonthDayTest() {
|
||||||
|
DateTime nextDateOfMonthDay = DateUtil.getLastDateOfMonthDay(2, 29);
|
||||||
|
System.out.println(nextDateOfMonthDay);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void getNextDateOfMonthDayTest() {
|
||||||
|
DateTime nextDateOfMonthDay = DateUtil.getNextDateOfMonthDay(2, 29);
|
||||||
|
System.out.println(nextDateOfMonthDay);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user