add method

This commit is contained in:
Looly 2022-01-26 17:11:42 +08:00
parent 201290d7c4
commit f729ba7b9c
5 changed files with 82 additions and 5 deletions

View File

@ -9,6 +9,8 @@
* 【extra 】 EmojiUtil增加方法pr#519@Gitee
* 【core 】 DateUtil 添加两个日期是否同一周方法pr#516@Gitee
* 【db 】 新增条件组用于处理复杂的where条件pr#514@Gitee
* 【core 】 新增LocalDateTimeUtil.weekOfYearissue#I4RWXC@Gitee
* 【core 】 Month增加toJdkMonth、getValueBaseOne
### 🐞Bug修复
* 【core 】 修复ChineseDate农历获取正月出现数组越界BUGissue#2112@Github

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}