mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add method
This commit is contained in:
parent
201290d7c4
commit
f729ba7b9c
@ -9,6 +9,8 @@
|
||||
* 【extra 】 EmojiUtil增加方法(pr#519@Gitee)
|
||||
* 【core 】 DateUtil 添加两个日期是否同一周方法(pr#516@Gitee)
|
||||
* 【db 】 新增条件组,用于处理复杂的where条件(pr#514@Gitee)
|
||||
* 【core 】 新增LocalDateTimeUtil.weekOfYear(issue#I4RWXC@Gitee)
|
||||
* 【core 】 Month增加toJdkMonth、getValueBaseOne
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复ChineseDate农历获取正月出现数组越界BUG(issue#2112@Github)
|
||||
|
@ -22,6 +22,7 @@ import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.Temporal;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.time.temporal.TemporalUnit;
|
||||
import java.time.temporal.WeekFields;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
@ -562,4 +563,21 @@ public class LocalDateTimeUtil {
|
||||
return startTime.isAfter(realEndTime) || endTime.isBefore(realStartTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定日期是所在年份的第几周,如:
|
||||
* <ul>
|
||||
* <li>如果一年的第一天是星期一,则第一周从第一天开始,没有零周</li>
|
||||
* <li>如果一年的第二天是星期一,则第一周从第二天开始,而第一天在零周</li>
|
||||
* <li>如果一年中的第4天是星期一,则第1周从第4周开始,第1至第3周在零周开始</li>
|
||||
* <li>如果一年中的第5天是星期一,则第二周从第5周开始,第1至第4周在第1周</li>
|
||||
* </ul>
|
||||
*
|
||||
*
|
||||
* @param date 日期({@link LocalDate} 或者 {@link LocalDateTime}等)
|
||||
* @return 所在年的第几周
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public static int weekOfYear(TemporalAccessor date){
|
||||
return TemporalAccessorUtil.get(date, WeekFields.ISO.weekOfYear());
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package cn.hutool.core.date;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
/**
|
||||
@ -93,14 +95,27 @@ public enum Month {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取{@link Calendar}中的对应值
|
||||
* 获取{@link Calendar}中的对应值<br>
|
||||
* 此值从0开始,即0表示一月
|
||||
*
|
||||
* @return {@link Calendar}中的对应值
|
||||
* @return {@link Calendar}中的对应月份值,从0开始计数
|
||||
*/
|
||||
public int getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取月份值,此值与{@link java.time.Month}对应<br>
|
||||
* 此值从1开始,即1表示一月
|
||||
*
|
||||
* @return 月份值,对应{@link java.time.Month},从1开始计数
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public int getValueBaseOne() {
|
||||
Assert.isFalse(this == UNDECIMBER, "Unsupported UNDECIMBER Field");
|
||||
return getValue() + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取此月份最后一天的值,不支持的月份(例如UNDECIMBER)返回-1
|
||||
*
|
||||
@ -165,17 +180,28 @@ public enum Month {
|
||||
|
||||
/**
|
||||
* 获得指定月的最后一天
|
||||
* @param month 月份,从0开始
|
||||
*
|
||||
* @param month 月份,从0开始
|
||||
* @param isLeapYear 是否为闰年,闰年只对二月有影响
|
||||
* @return 最后一天,可能为28,29,30,31
|
||||
* @since 5.4.7
|
||||
*/
|
||||
public static int getLastDay(int month, boolean isLeapYear){
|
||||
public static int getLastDay(int month, boolean isLeapYear) {
|
||||
int lastDay = DAYS_OF_MONTH[month];
|
||||
if (isLeapYear && Calendar.FEBRUARY == month){
|
||||
if (isLeapYear && Calendar.FEBRUARY == month) {
|
||||
// 二月
|
||||
lastDay += 1;
|
||||
}
|
||||
return lastDay;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为{@link java.time.Month}
|
||||
*
|
||||
* @return {@link java.time.Month}
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public java.time.Month toJdkMonth() {
|
||||
return java.time.Month.of(getValueBaseOne());
|
||||
}
|
||||
}
|
||||
|
@ -197,4 +197,24 @@ public class LocalDateTimeUtilTest {
|
||||
Assert.assertTrue(LocalDateTimeUtil.isOverlap(oneStartTime2,oneEndTime2,realStartTime,realEndTime));
|
||||
Assert.assertFalse(LocalDateTimeUtil.isOverlap(oneStartTime3,oneEndTime3,realStartTime,realEndTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void weekOfYearTest(){
|
||||
LocalDate date1 = LocalDate.of(2021, 12, 31);
|
||||
final int weekOfYear1 = LocalDateTimeUtil.weekOfYear(date1);
|
||||
Assert.assertEquals(weekOfYear1, 52);
|
||||
|
||||
final int weekOfYear2 = LocalDateTimeUtil.weekOfYear(date1.atStartOfDay());
|
||||
Assert.assertEquals(weekOfYear2, 52);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void weekOfYearTest2(){
|
||||
LocalDate date1 = LocalDate.of(2022, 1, 31);
|
||||
final int weekOfYear1 = LocalDateTimeUtil.weekOfYear(date1);
|
||||
Assert.assertEquals(weekOfYear1, 52);
|
||||
|
||||
final int weekOfYear2 = LocalDateTimeUtil.weekOfYear(date1.atStartOfDay());
|
||||
Assert.assertEquals(weekOfYear2, 52);
|
||||
}
|
||||
}
|
||||
|
@ -37,4 +37,15 @@ public class MonthTest {
|
||||
lastDay = Month.of(Calendar.DECEMBER).getLastDay(true);
|
||||
Assert.assertEquals(31, lastDay);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toJdkMonthTest(){
|
||||
final java.time.Month month = Month.AUGUST.toJdkMonth();
|
||||
Assert.assertEquals(java.time.Month.AUGUST, month);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void toJdkMonthTest2(){
|
||||
Month.UNDECIMBER.toJdkMonth();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user