Skip to content

Commit

Permalink
v1.3.13 修复节气当天获取下一节气仍为当前节气的问题;修复每日宜忌存在重复项的问题;修复八字转阳历存在遗漏的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Feb 29, 2024
1 parent 1c3ff0c commit 0a4d172
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@

## [1.3.12] - 2024-02-06
1. 八字转阳历结果按时间先后排序,转换速度大幅提升。

## [1.3.13] - 2024-02-29
1. 修复节气当天获取下一节气仍为当前节气的问题。
2. 修复每日宜忌存在重复项的问题。
3. 修复八字转阳历存在遗漏的问题。
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.3.12</version>
<version>1.3.13</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
<dependency>
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<version>1.3.12</version>
<version>1.3.13</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.6tail</groupId>
<artifactId>lunar</artifactId>
<packaging>jar</packaging>
<version>1.3.12</version>
<version>1.3.13</version>
<name>${project.groupId}:${project.artifactId}</name>
<url>https://github.com/6tail/lunar-java</url>
<description>a calendar library for Solar and Chinese Lunar</description>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/nlf/calendar/Lunar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2380,7 +2380,7 @@ protected JieQi getNearJieQi(boolean forward, String[] conditions, boolean whole
Solar solar = entry.getValue();
String day = wholeDay ? solar.toYmd() : solar.toYmdHms();
if (forward) {
if (day.compareTo(today) < 0) {
if (day.compareTo(today) <= 0) {
continue;
}
if (null == near) {
Expand Down
47 changes: 31 additions & 16 deletions src/main/java/com/nlf/calendar/Solar.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String
m += 12;
}
// 月天干要一致
if (((LunarUtil.find(yearGanZhi.substring(0, 1), LunarUtil.GAN, -1) + 1) * 2 + m) % 10 != LunarUtil.find(monthGanZhi.substring(0,1), LunarUtil.GAN, -1)) {
if (((LunarUtil.find(yearGanZhi.substring(0, 1), LunarUtil.GAN, -1) + 1) * 2 + m) % 10 != LunarUtil.find(monthGanZhi.substring(0, 1), LunarUtil.GAN, -1)) {
return l;
}
// 1年的立春是辛酉,序号57
Expand All @@ -315,6 +315,10 @@ public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String
m *= 2;
// 时辰地支转时刻,子时按零点算
int h = LunarUtil.find(timeGanZhi.substring(1), LunarUtil.ZHI, -1) * 2;
int[] hours = {h};
if (0 == h && 2 == sect) {
hours = new int[]{0, 23};
}
int startYear = baseYear - 1;

// 结束年
Expand All @@ -330,8 +334,6 @@ public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String
// 节令推移,年干支和月干支就都匹配上了
Solar solarTime = jieQiList.get(4 + m);
if (solarTime.getYear() >= baseYear) {
int mi = 0;
int s = 0;
// 日干支和节令干支的偏移值
Lunar lunar = solarTime.getLunar();
String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
Expand All @@ -342,17 +344,22 @@ public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String
if (d > 0) {
// 从节令推移天数
solarTime = solarTime.next(d);
} else if (h == solarTime.getHour()) {
// 如果正好是节令当天,且小时和节令的小时数相等的极端情况,把分钟和秒钟带上
mi = solarTime.getMinute();
s = solarTime.getSecond();
}
// 验证一下
Solar solar = Solar.fromYmdHms(solarTime.getYear(), solarTime.getMonth(), solarTime.getDay(), h, mi, s);
lunar = solar.getLunar();
dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi) && dgz.equals(dayGanZhi) && lunar.getTimeInGanZhi().equals(timeGanZhi)) {
l.add(solar);
for (int hour : hours) {
int mi = 0;
int s = 0;
if (d == 0 && hour == solarTime.getHour()) {
// 如果正好是节令当天,且小时和节令的小时数相等的极端情况,把分钟和秒钟带上
mi = solarTime.getMinute();
s = solarTime.getSecond();
}
// 验证一下
Solar solar = Solar.fromYmdHms(solarTime.getYear(), solarTime.getMonth(), solarTime.getDay(), hour, mi, s);
lunar = solar.getLunar();
dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi) && dgz.equals(dayGanZhi) && lunar.getTimeInGanZhi().equals(timeGanZhi)) {
l.add(solar);
}
}
}
}
Expand All @@ -376,7 +383,7 @@ public boolean isLeapYear() {
* @return 0123456
*/
public int getWeek() {
return ((int)(getJulianDay() + 0.5) + 7000001) % 7;
return ((int) (getJulianDay() + 0.5) + 7000001) % 7;
}

/**
Expand Down Expand Up @@ -601,6 +608,7 @@ public String toFullString() {

/**
* 阳历日期相减,获得相差天数
*
* @param solar 阳历
* @return 天数
*/
Expand All @@ -610,6 +618,7 @@ public int subtract(Solar solar) {

/**
* 阳历日期相减,获得相差分钟数
*
* @param solar 阳历
* @return 分钟数
*/
Expand All @@ -628,6 +637,7 @@ public int subtractMinute(Solar solar) {

/**
* 是否在指定日期之后
*
* @param solar 阳历
* @return true/false
*/
Expand Down Expand Up @@ -667,6 +677,7 @@ public boolean isAfter(Solar solar) {

/**
* 是否在指定日期之前
*
* @param solar 阳历
* @return true/false
*/
Expand Down Expand Up @@ -706,6 +717,7 @@ public boolean isBefore(Solar solar) {

/**
* 年推移
*
* @param years 年数
* @return 阳历
*/
Expand All @@ -729,6 +741,7 @@ public Solar nextYear(int years) {

/**
* 月推移
*
* @param months 月数
* @return 阳历
*/
Expand Down Expand Up @@ -789,7 +802,7 @@ public Solar next(int days) {
d += days;
}
if (1582 == y && 10 == m) {
if (d > 4 ) {
if (d > 4) {
d += 10;
}
}
Expand All @@ -804,7 +817,7 @@ public Solar next(int days) {
* @return 阳历日期
*/
public Solar next(int days, boolean onlyWorkday) {
if(!onlyWorkday) {
if (!onlyWorkday) {
return next(days);
}
Solar solar = fromYmdHms(year, month, day, hour, minute, second);
Expand Down Expand Up @@ -833,6 +846,7 @@ public Solar next(int days, boolean onlyWorkday) {

/**
* 小时推移
*
* @param hours 小时数
* @return 阳历
*/
Expand All @@ -852,6 +866,7 @@ public Solar nextHour(int hours) {

/**
* 获取薪资比例(感谢 https://gitee.com/smr1987)
*
* @return 薪资比例:1/2/3
*/
public int getSalaryRate() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/nlf/calendar/util/LunarUtil.java

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions src/test/java/test/BaZiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.nlf.calendar.EightChar;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;
import com.nlf.calendar.eightchar.Yun;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -493,4 +494,40 @@ public void test27() {
Assert.assertEquals("身宫", "乙巳", eightChar.getShenGong());
}

@Test
public void test28() {
Lunar lunar = Solar.fromYmdHms(1986, 5, 29, 13, 37, 0).getLunar();
EightChar eightChar = lunar.getEightChar();
Yun yun = eightChar.getYun(1, 2);
Assert.assertEquals(2, yun.getStartYear());
Assert.assertEquals(7, yun.getStartMonth());
Assert.assertEquals(0, yun.getStartDay());
}

@Test
public void test29() {
List<Solar> l = Solar.fromBaZi("甲辰","丙寅","壬戌","壬子", 2);
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}

List<String> expected = new ArrayList<String>();
expected.add("2024-02-28 23:00:00");
Assert.assertEquals(expected, actual);
}

@Test
public void test30() {
List<Solar> l = Solar.fromBaZi("甲辰","丙寅","癸亥","壬子", 1);
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}

List<String> expected = new ArrayList<String>();
expected.add("2024-02-29 00:00:00");
Assert.assertEquals(expected, actual);
}

}

0 comments on commit 0a4d172

Please sign in to comment.