mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
CalendarUtil
增加isSameYear
和calendar
方法(issue#3995@Github)
This commit is contained in:
@@ -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)
|
||||||
|
@@ -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>
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user