CalendarUtil增加isSameYearcalendar方法(issue#3995@Github)

This commit is contained in:
Looly
2025-07-24 16:16:32 +08:00
parent 3d0d8dea4b
commit a09da9fd3d
2 changed files with 41 additions and 1 deletions

View File

@@ -2,11 +2,12 @@
# 🚀Changelog # 🚀Changelog
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.40(2025-07-21) # 5.8.40(2025-07-24)
### 🐣新特性 ### 🐣新特性
* 【captcha】 `MathGenerator`四则运算方式支持不生成负数结果pr#1363@Gitee * 【captcha】 `MathGenerator`四则运算方式支持不生成负数结果pr#1363@Gitee
* 【core 】 增加`MapValueProvider``RecordConverter`并支持Record转换issue#3985@Github * 【core 】 增加`MapValueProvider``RecordConverter`并支持Record转换issue#3985@Github
* 【core 】 `CalendarUtil`增加`isSameYear``calendar`方法issue#3995@Github
### 🐞Bug修复 ### 🐞Bug修复
* 【extra 】 `Sftp``reconnectIfTimeout`方法改为捕获所有异常issue#3989@Github * 【extra 】 `Sftp``reconnectIfTimeout`方法改为捕获所有异常issue#3989@Github

View File

@@ -74,6 +74,21 @@ public class CalendarUtil {
return cal; return cal;
} }
/**
* 转换为指定时区的Calendar返回新的Calendar
*
* @param calendar 时间
* @param timeZone 新时区
* @return 指定时区的新的calendar对象
* @since 5.8.30
*/
public static Calendar calendar(Calendar calendar, final TimeZone timeZone) {
// 转换到统一时区例如UTC
calendar = (Calendar) calendar.clone();
calendar.setTimeZone(timeZone);
return calendar;
}
/** /**
* 是否为上午 * 是否为上午
* *
@@ -437,6 +452,30 @@ public class CalendarUtil {
cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA); cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA);
} }
/**
* 比较两个日期是否为同一年<br>
* 同一个年的意思是ERA公元、year都一致。
*
* @param cal1 日期1
* @param cal2 日期2
* @return 是否为同一年
* @since 5.8.30
*/
public static boolean isSameYear(final Calendar cal1, Calendar cal2) {
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The date must not be null");
}
if (!ObjUtil.equals(cal1.getTimeZone(), cal2.getTimeZone())) {
// 统一时区
cal2 = calendar(cal2, cal1.getTimeZone());
}
return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && //
// issue#3011@Github
cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA);
}
/** /**
* <p>检查两个Calendar时间戳是否相同。</p> * <p>检查两个Calendar时间戳是否相同。</p>
* *