This commit is contained in:
Looly 2022-03-28 00:44:21 +08:00
parent 8177848a47
commit 63ad6de20d
6 changed files with 52 additions and 16 deletions

View File

@ -65,6 +65,7 @@
* 【core 】 FileUtil.getMimeType增加rar、7z支持issue#I4ZBN0@Gitee
* 【json 】 JSON修复transient设置无效问题issue#2212@Github
* 【core 】 修复IterUtil.getElementType获取结果为null的问题issue#2222@Github
* 【core 】 修复农历转公历在闰月时错误issue#I4ZSGJ@Gitee
-------------------------------------------------------------------------------------------------------------
# 5.7.22 (2022-03-01)

View File

@ -113,7 +113,8 @@ public class ChineseDate {
}
/**
* 构造方法传入日期
* 构造方法传入日期<br>
* 此方法自动判断闰月如果chineseMonth为本年的闰月则按照闰月计算
*
* @param chineseYear 农历年
* @param chineseMonth 农历月1表示一月正月
@ -121,28 +122,28 @@ public class ChineseDate {
* @since 5.2.4
*/
public ChineseDate(int chineseYear, int chineseMonth, int chineseDay) {
this(chineseYear, chineseMonth, chineseDay, chineseMonth == LunarInfo.leapMonth(chineseYear) + 1);
this(chineseYear, chineseMonth, chineseDay, chineseMonth == LunarInfo.leapMonth(chineseYear));
}
/**
* 构造方法传入日期
* 构造方法传入日期<br>
* 通过isLeapMonth参数区分是否闰月如五月是闰月当isLeapMonth为{@code true}表示润五月{@code false}表示五月
*
* @param chineseYear 农历年
* @param chineseMonth 农历月1表示一月正月
* @param chineseMonth 农历月1表示一月正月如果isLeapMonth为{@code true}1表示润一月
* @param chineseDay 农历日1表示初一
* @param isLeapMonth 当前月份是否闰月
* @since 5.7.18
*/
public ChineseDate(int chineseYear, int chineseMonth, int chineseDay, boolean isLeapMonth) {
this.day = chineseDay;
this.month = chineseMonth;
// 当月是闰月的后边的月定义为闰月如润的是五月则5表示五月6表示润五月
this.isLeapMonth = isLeapMonth;
// 闰月时农历月份+1如6表示润五月
this.month = isLeapMonth ? chineseMonth + 1 : chineseMonth;
this.year = chineseYear;
//先判断传入的月份是不是闰月
int leapMonth = LunarInfo.leapMonth(chineseYear);
final DateTime dateTime = lunar2solar(chineseYear, chineseMonth, chineseDay, chineseMonth == leapMonth);
final DateTime dateTime = lunar2solar(chineseYear, chineseMonth, chineseDay, isLeapMonth);
if (null != dateTime) {
//初始化公历年
this.gday = dateTime.dayOfMonth();

View File

@ -145,7 +145,7 @@ public enum Week {
/**
* {@link Calendar}星期相关值转换为Week枚举对象<br>
*
* @param calendarWeekIntValue Calendar中关于Week的int值
* @param calendarWeekIntValue Calendar中关于Week的int值1表示Sunday
* @return Week
* @see #SUNDAY
* @see #MONDAY
@ -156,10 +156,10 @@ public enum Week {
* @see #SATURDAY
*/
public static Week of(int calendarWeekIntValue) {
if (calendarWeekIntValue >= ENUMS.length || calendarWeekIntValue < 0) {
if (calendarWeekIntValue > ENUMS.length || calendarWeekIntValue < 1) {
return null;
}
return ENUMS[calendarWeekIntValue];
return ENUMS[calendarWeekIntValue - 1];
}
/**
@ -172,7 +172,7 @@ public enum Week {
*/
public static Week of(String name) throws IllegalArgumentException {
Assert.notBlank(name);
Week of = of(ArrayUtil.indexOfIgnoreCase(ALIASES, name));
Week of = of(ArrayUtil.indexOfIgnoreCase(ALIASES, name) + 1);
if (null == of) {
of = Week.valueOf(name.toUpperCase());
}
@ -195,8 +195,9 @@ public enum Week {
*/
public static Week of(DayOfWeek dayOfWeek) {
Assert.notNull(dayOfWeek);
int week = dayOfWeek.ordinal() + 1;
if (7 == week) {
int week = dayOfWeek.getValue() + 1;
if(8 == week){
// 周日
week = 1;
}
return of(week);

View File

@ -57,10 +57,12 @@ public class ChineseDateTest {
Assert.assertEquals("六月", chineseDate.getChineseMonth());
chineseDate = new ChineseDate(2020,4,15);
Assert.assertEquals("四月", chineseDate.getChineseMonth());
Assert.assertEquals("2020-06-06 00:00:00", chineseDate.getGregorianDate().toString());
Assert.assertEquals("闰四月", chineseDate.getChineseMonth());
chineseDate = new ChineseDate(2020,5,15);
Assert.assertEquals("闰四月", chineseDate.getChineseMonth());
Assert.assertEquals("2020-07-05 00:00:00", chineseDate.getGregorianDate().toString());
Assert.assertEquals("五月", chineseDate.getChineseMonth());
}
@Test
@ -82,12 +84,14 @@ public class ChineseDateTest {
@Test
public void dateTest2(){
//noinspection ConstantConditions
ChineseDate date = new ChineseDate(DateUtil.parse("2020-10-19"));
Assert.assertEquals("庚子鼠年 九月初三", date.toString());
}
@Test
public void dateTest2_2(){
//noinspection ConstantConditions
ChineseDate date = new ChineseDate(DateUtil.parse("2020-07-20"));
Assert.assertEquals("庚子鼠年 五月三十", date.toString());
}
@ -95,13 +99,16 @@ public class ChineseDateTest {
@Test
public void dateTest3(){
// 初一offset为0测试
//noinspection ConstantConditions
ChineseDate date = new ChineseDate(DateUtil.parse("2099-03-22"));
Assert.assertEquals("己未羊年 闰二月初一", date.toString());
}
@Test
public void leapMonthTest(){
//noinspection ConstantConditions
final ChineseDate c1 = new ChineseDate(DateUtil.parse("2028-05-28"));
//noinspection ConstantConditions
final ChineseDate c2 = new ChineseDate(DateUtil.parse("2028-06-27"));
Assert.assertEquals("戊申猴年 五月初五", c1.toString());
@ -120,14 +127,17 @@ public class ChineseDateTest {
public void day19700101Test(){
// https://gitee.com/dromara/hutool/issues/I4UTPK
Date date = DateUtil.parse("1970-01-01");
//noinspection ConstantConditions
ChineseDate chineseDate = new ChineseDate(date);
Assert.assertEquals("己酉鸡年 冬月廿四", chineseDate.toString());
date = DateUtil.parse("1970-01-02");
//noinspection ConstantConditions
chineseDate = new ChineseDate(date);
Assert.assertEquals("己酉鸡年 冬月廿五", chineseDate.toString());
date = DateUtil.parse("1970-01-03");
//noinspection ConstantConditions
chineseDate = new ChineseDate(date);
Assert.assertEquals("己酉鸡年 冬月廿六", chineseDate.toString());
}
@ -136,7 +146,18 @@ public class ChineseDateTest {
public void day19000101Test(){
// 1900-01-31之前不支持
Date date = DateUtil.parse("1900-01-31");
//noinspection ConstantConditions
ChineseDate chineseDate = new ChineseDate(date);
Assert.assertEquals("庚子鼠年 正月初一", chineseDate.toString());
}
@Test
public void getGregorianDateTest(){
// https://gitee.com/dromara/hutool/issues/I4ZSGJ
ChineseDate chineseDate = new ChineseDate(1998, 5, 1);
Assert.assertEquals("1998-06-24 00:00:00", chineseDate.getGregorianDate().toString());
chineseDate = new ChineseDate(1998, 5, 1, false);
Assert.assertEquals("1998-05-26 00:00:00", chineseDate.getGregorianDate().toString());
}
}

View File

@ -37,6 +37,7 @@ public class WeekTest {
Assert.assertEquals(Week.SATURDAY, Week.of("SATURDAY"));
}
@Test
public void ofTest2(){
Assert.assertEquals(Week.SUNDAY, Week.of(DayOfWeek.SUNDAY));
Assert.assertEquals(Week.MONDAY, Week.of(DayOfWeek.MONDAY));

View File

@ -1,8 +1,10 @@
package cn.hutool.cron.pattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Calendar;
@ -19,4 +21,13 @@ public class CronPatternNextMatchTest {
Console.log(DateUtil.date(calendar));
Assert.assertTrue(pattern.match(calendar, true));
}
@Test
@Ignore
public void calendarTest(){
final Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_WEEK, Week.SATURDAY.getValue());
Console.log(DateUtil.date(ca));
}
}