mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix bug
This commit is contained in:
parent
8177848a47
commit
63ad6de20d
@ -65,6 +65,7 @@
|
|||||||
* 【core 】 FileUtil.getMimeType增加rar、7z支持(issue#I4ZBN0@Gitee)
|
* 【core 】 FileUtil.getMimeType增加rar、7z支持(issue#I4ZBN0@Gitee)
|
||||||
* 【json 】 JSON修复transient设置无效问题(issue#2212@Github)
|
* 【json 】 JSON修复transient设置无效问题(issue#2212@Github)
|
||||||
* 【core 】 修复IterUtil.getElementType获取结果为null的问题(issue#2222@Github)
|
* 【core 】 修复IterUtil.getElementType获取结果为null的问题(issue#2222@Github)
|
||||||
|
* 【core 】 修复农历转公历在闰月时错误(issue#I4ZSGJ@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.7.22 (2022-03-01)
|
# 5.7.22 (2022-03-01)
|
||||||
|
@ -113,7 +113,8 @@ public class ChineseDate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造方法传入日期
|
* 构造方法传入日期<br>
|
||||||
|
* 此方法自动判断闰月,如果chineseMonth为本年的闰月,则按照闰月计算
|
||||||
*
|
*
|
||||||
* @param chineseYear 农历年
|
* @param chineseYear 农历年
|
||||||
* @param chineseMonth 农历月,1表示一月(正月)
|
* @param chineseMonth 农历月,1表示一月(正月)
|
||||||
@ -121,28 +122,28 @@ public class ChineseDate {
|
|||||||
* @since 5.2.4
|
* @since 5.2.4
|
||||||
*/
|
*/
|
||||||
public ChineseDate(int chineseYear, int chineseMonth, int chineseDay) {
|
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 chineseYear 农历年
|
||||||
* @param chineseMonth 农历月,1表示一月(正月)
|
* @param chineseMonth 农历月,1表示一月(正月),如果isLeapMonth为{@code true},1表示润一月
|
||||||
* @param chineseDay 农历日,1表示初一
|
* @param chineseDay 农历日,1表示初一
|
||||||
* @param isLeapMonth 当前月份是否闰月
|
* @param isLeapMonth 当前月份是否闰月
|
||||||
* @since 5.7.18
|
* @since 5.7.18
|
||||||
*/
|
*/
|
||||||
public ChineseDate(int chineseYear, int chineseMonth, int chineseDay, boolean isLeapMonth) {
|
public ChineseDate(int chineseYear, int chineseMonth, int chineseDay, boolean isLeapMonth) {
|
||||||
this.day = chineseDay;
|
this.day = chineseDay;
|
||||||
this.month = chineseMonth;
|
|
||||||
// 当月是闰月的后边的月定义为闰月,如润的是五月,则5表示五月,6表示润五月
|
// 当月是闰月的后边的月定义为闰月,如润的是五月,则5表示五月,6表示润五月
|
||||||
this.isLeapMonth = isLeapMonth;
|
this.isLeapMonth = isLeapMonth;
|
||||||
|
// 闰月时,农历月份+1,如6表示润五月
|
||||||
|
this.month = isLeapMonth ? chineseMonth + 1 : chineseMonth;
|
||||||
this.year = chineseYear;
|
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) {
|
if (null != dateTime) {
|
||||||
//初始化公历年
|
//初始化公历年
|
||||||
this.gday = dateTime.dayOfMonth();
|
this.gday = dateTime.dayOfMonth();
|
||||||
|
@ -145,7 +145,7 @@ public enum Week {
|
|||||||
/**
|
/**
|
||||||
* 将 {@link Calendar}星期相关值转换为Week枚举对象<br>
|
* 将 {@link Calendar}星期相关值转换为Week枚举对象<br>
|
||||||
*
|
*
|
||||||
* @param calendarWeekIntValue Calendar中关于Week的int值
|
* @param calendarWeekIntValue Calendar中关于Week的int值,1表示Sunday
|
||||||
* @return Week
|
* @return Week
|
||||||
* @see #SUNDAY
|
* @see #SUNDAY
|
||||||
* @see #MONDAY
|
* @see #MONDAY
|
||||||
@ -156,10 +156,10 @@ public enum Week {
|
|||||||
* @see #SATURDAY
|
* @see #SATURDAY
|
||||||
*/
|
*/
|
||||||
public static Week of(int calendarWeekIntValue) {
|
public static Week of(int calendarWeekIntValue) {
|
||||||
if (calendarWeekIntValue >= ENUMS.length || calendarWeekIntValue < 0) {
|
if (calendarWeekIntValue > ENUMS.length || calendarWeekIntValue < 1) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return ENUMS[calendarWeekIntValue];
|
return ENUMS[calendarWeekIntValue - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -172,7 +172,7 @@ public enum Week {
|
|||||||
*/
|
*/
|
||||||
public static Week of(String name) throws IllegalArgumentException {
|
public static Week of(String name) throws IllegalArgumentException {
|
||||||
Assert.notBlank(name);
|
Assert.notBlank(name);
|
||||||
Week of = of(ArrayUtil.indexOfIgnoreCase(ALIASES, name));
|
Week of = of(ArrayUtil.indexOfIgnoreCase(ALIASES, name) + 1);
|
||||||
if (null == of) {
|
if (null == of) {
|
||||||
of = Week.valueOf(name.toUpperCase());
|
of = Week.valueOf(name.toUpperCase());
|
||||||
}
|
}
|
||||||
@ -195,8 +195,9 @@ public enum Week {
|
|||||||
*/
|
*/
|
||||||
public static Week of(DayOfWeek dayOfWeek) {
|
public static Week of(DayOfWeek dayOfWeek) {
|
||||||
Assert.notNull(dayOfWeek);
|
Assert.notNull(dayOfWeek);
|
||||||
int week = dayOfWeek.ordinal() + 1;
|
int week = dayOfWeek.getValue() + 1;
|
||||||
if (7 == week) {
|
if(8 == week){
|
||||||
|
// 周日
|
||||||
week = 1;
|
week = 1;
|
||||||
}
|
}
|
||||||
return of(week);
|
return of(week);
|
||||||
|
@ -57,10 +57,12 @@ public class ChineseDateTest {
|
|||||||
Assert.assertEquals("六月", chineseDate.getChineseMonth());
|
Assert.assertEquals("六月", chineseDate.getChineseMonth());
|
||||||
|
|
||||||
chineseDate = new ChineseDate(2020,4,15);
|
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);
|
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
|
@Test
|
||||||
@ -82,12 +84,14 @@ public class ChineseDateTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dateTest2(){
|
public void dateTest2(){
|
||||||
|
//noinspection ConstantConditions
|
||||||
ChineseDate date = new ChineseDate(DateUtil.parse("2020-10-19"));
|
ChineseDate date = new ChineseDate(DateUtil.parse("2020-10-19"));
|
||||||
Assert.assertEquals("庚子鼠年 九月初三", date.toString());
|
Assert.assertEquals("庚子鼠年 九月初三", date.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dateTest2_2(){
|
public void dateTest2_2(){
|
||||||
|
//noinspection ConstantConditions
|
||||||
ChineseDate date = new ChineseDate(DateUtil.parse("2020-07-20"));
|
ChineseDate date = new ChineseDate(DateUtil.parse("2020-07-20"));
|
||||||
Assert.assertEquals("庚子鼠年 五月三十", date.toString());
|
Assert.assertEquals("庚子鼠年 五月三十", date.toString());
|
||||||
}
|
}
|
||||||
@ -95,13 +99,16 @@ public class ChineseDateTest {
|
|||||||
@Test
|
@Test
|
||||||
public void dateTest3(){
|
public void dateTest3(){
|
||||||
// 初一,offset为0测试
|
// 初一,offset为0测试
|
||||||
|
//noinspection ConstantConditions
|
||||||
ChineseDate date = new ChineseDate(DateUtil.parse("2099-03-22"));
|
ChineseDate date = new ChineseDate(DateUtil.parse("2099-03-22"));
|
||||||
Assert.assertEquals("己未羊年 闰二月初一", date.toString());
|
Assert.assertEquals("己未羊年 闰二月初一", date.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void leapMonthTest(){
|
public void leapMonthTest(){
|
||||||
|
//noinspection ConstantConditions
|
||||||
final ChineseDate c1 = new ChineseDate(DateUtil.parse("2028-05-28"));
|
final ChineseDate c1 = new ChineseDate(DateUtil.parse("2028-05-28"));
|
||||||
|
//noinspection ConstantConditions
|
||||||
final ChineseDate c2 = new ChineseDate(DateUtil.parse("2028-06-27"));
|
final ChineseDate c2 = new ChineseDate(DateUtil.parse("2028-06-27"));
|
||||||
|
|
||||||
Assert.assertEquals("戊申猴年 五月初五", c1.toString());
|
Assert.assertEquals("戊申猴年 五月初五", c1.toString());
|
||||||
@ -120,14 +127,17 @@ public class ChineseDateTest {
|
|||||||
public void day19700101Test(){
|
public void day19700101Test(){
|
||||||
// https://gitee.com/dromara/hutool/issues/I4UTPK
|
// https://gitee.com/dromara/hutool/issues/I4UTPK
|
||||||
Date date = DateUtil.parse("1970-01-01");
|
Date date = DateUtil.parse("1970-01-01");
|
||||||
|
//noinspection ConstantConditions
|
||||||
ChineseDate chineseDate = new ChineseDate(date);
|
ChineseDate chineseDate = new ChineseDate(date);
|
||||||
Assert.assertEquals("己酉鸡年 冬月廿四", chineseDate.toString());
|
Assert.assertEquals("己酉鸡年 冬月廿四", chineseDate.toString());
|
||||||
|
|
||||||
date = DateUtil.parse("1970-01-02");
|
date = DateUtil.parse("1970-01-02");
|
||||||
|
//noinspection ConstantConditions
|
||||||
chineseDate = new ChineseDate(date);
|
chineseDate = new ChineseDate(date);
|
||||||
Assert.assertEquals("己酉鸡年 冬月廿五", chineseDate.toString());
|
Assert.assertEquals("己酉鸡年 冬月廿五", chineseDate.toString());
|
||||||
|
|
||||||
date = DateUtil.parse("1970-01-03");
|
date = DateUtil.parse("1970-01-03");
|
||||||
|
//noinspection ConstantConditions
|
||||||
chineseDate = new ChineseDate(date);
|
chineseDate = new ChineseDate(date);
|
||||||
Assert.assertEquals("己酉鸡年 冬月廿六", chineseDate.toString());
|
Assert.assertEquals("己酉鸡年 冬月廿六", chineseDate.toString());
|
||||||
}
|
}
|
||||||
@ -136,7 +146,18 @@ public class ChineseDateTest {
|
|||||||
public void day19000101Test(){
|
public void day19000101Test(){
|
||||||
// 1900-01-31之前不支持
|
// 1900-01-31之前不支持
|
||||||
Date date = DateUtil.parse("1900-01-31");
|
Date date = DateUtil.parse("1900-01-31");
|
||||||
|
//noinspection ConstantConditions
|
||||||
ChineseDate chineseDate = new ChineseDate(date);
|
ChineseDate chineseDate = new ChineseDate(date);
|
||||||
Assert.assertEquals("庚子鼠年 正月初一", chineseDate.toString());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ public class WeekTest {
|
|||||||
Assert.assertEquals(Week.SATURDAY, Week.of("SATURDAY"));
|
Assert.assertEquals(Week.SATURDAY, Week.of("SATURDAY"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void ofTest2(){
|
public void ofTest2(){
|
||||||
Assert.assertEquals(Week.SUNDAY, Week.of(DayOfWeek.SUNDAY));
|
Assert.assertEquals(Week.SUNDAY, Week.of(DayOfWeek.SUNDAY));
|
||||||
Assert.assertEquals(Week.MONDAY, Week.of(DayOfWeek.MONDAY));
|
Assert.assertEquals(Week.MONDAY, Week.of(DayOfWeek.MONDAY));
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package cn.hutool.cron.pattern;
|
package cn.hutool.cron.pattern;
|
||||||
|
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import cn.hutool.core.date.Week;
|
||||||
import cn.hutool.core.lang.Console;
|
import cn.hutool.core.lang.Console;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
@ -19,4 +21,13 @@ public class CronPatternNextMatchTest {
|
|||||||
Console.log(DateUtil.date(calendar));
|
Console.log(DateUtil.date(calendar));
|
||||||
Assert.assertTrue(pattern.match(calendar, true));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user