Skip to content

Commit

Permalink
v1.3.3 优化八字转阳历;修复少量农历年的月份错误;新增农历年天数和当年月份列表。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Mar 11, 2023
1 parent 60dde2c commit 586a8d3
Show file tree
Hide file tree
Showing 12 changed files with 338 additions and 66 deletions.
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.2</version>
<version>1.3.3</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.2</version>
<version>1.3.3</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.2</version>
<version>1.3.3</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
7 changes: 2 additions & 5 deletions src/main/java/com/nlf/calendar/Lunar.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@ public Lunar(int lunarYear, int lunarMonth, int lunarDay, int hour, int minute,
public Lunar(Solar solar) {
LunarYear ly = LunarYear.fromYear(solar.getYear());
for (LunarMonth m : ly.getMonths()) {
// 初一
Solar firstDay = Solar.fromJulianDay(m.getFirstJulianDay());
int days = solar.subtract(firstDay);
int days = solar.subtract(Solar.fromJulianDay(m.getFirstJulianDay()));
if (days < m.getDayCount()) {
year = m.getYear();
month = m.getMonth();
Expand Down Expand Up @@ -2899,8 +2897,7 @@ public ShuJiu getShuJiu() {
start = Solar.fromYmd(start.getYear(), start.getMonth(), start.getDay());
}

Solar end = Solar.fromYmd(start.getYear(), start.getMonth(), start.getDay());
end = end.next(81);
Solar end = Solar.fromYmd(start.getYear(), start.getMonth(), start.getDay()).next(81);

if (current.isBefore(start) || !current.isBefore(end)) {
return null;
Expand Down
79 changes: 56 additions & 23 deletions src/main/java/com/nlf/calendar/LunarYear.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ private void compute() {
// 冬至前的初一
double w = ShouXingUtil.calcShuo(jq[0]);
if (w > jq[0]) {
w -= 29.5306;
if (currentYear != 41 && currentYear != 193 && currentYear != 288 && currentYear != 345 && currentYear != 918 && currentYear != 1013) {
w -= 29.5306;
}
}
// 递推每月初一
for (int i = 0, j = hs.length; i < j; i++) {
Expand All @@ -145,39 +147,41 @@ private void compute() {
dayCounts[i] = (int) (hs[i + 1] - hs[i]);
}

Integer currentYearLeap = LEAP.get(currentYear);
if (null == currentYearLeap) {
currentYearLeap = -1;
if (hs[13] <= jq[24]) {
int i = 1;
while (hs[i + 1] > jq[2 * i] && i < 13) {
i++;
int prevYear = currentYear - 1;

int leapYear = -1;
int leapIndex = -1;

Integer leap = LEAP.get(currentYear);
if (null == leap) {
leap = LEAP.get(prevYear);
if (null == leap) {
if (hs[13] <= jq[24]) {
int i = 1;
while (hs[i + 1] > jq[2 * i] && i < 13) {
i++;
}
leapYear = currentYear;
leapIndex = i;
}
currentYearLeap = i;
} else {
leapYear = prevYear;
leapIndex = leap - 12;
}
} else {
leapYear = currentYear;
leapIndex = leap;
}

int prevYear = currentYear - 1;
Integer prevYearLeap = LEAP.get(prevYear);
prevYearLeap = null == prevYearLeap ? -1 : prevYearLeap - 12;

int y = prevYear;
int m = 11;
for (int i = 0, j = dayCounts.length; i < j; i++) {
int cm = m;
boolean isNextLeap = false;
if (y == currentYear && i == currentYearLeap) {
cm = -cm;
} else if (y == prevYear && i == prevYearLeap) {
if (y == leapYear && i == leapIndex) {
cm = -cm;
}
if (y == currentYear && i + 1 == currentYearLeap) {
isNextLeap = true;
} else if (y == prevYear && i + 1 == prevYearLeap) {
isNextLeap = true;
}
this.months.add(new LunarMonth(y, cm, dayCounts[i], hs[i] + Solar.J2000));
if (!isNextLeap) {
if (y != leapYear || i + 1 != leapIndex) {
m++;
}
if (m == 13) {
Expand All @@ -196,6 +200,35 @@ public int getYear() {
return year;
}

/**
* 获取总天数
* @return 天数
*/
public int getDayCount() {
int n = 0;
for (LunarMonth m : months) {
if (m.getYear() == year) {
n += m.getDayCount();
}
}
return n;
}

/**
* 获取当年的农历月们
*
* @return 农历月们
*/
public List<LunarMonth> getMonthsInYear() {
List<LunarMonth> l = new ArrayList<LunarMonth>();
for (LunarMonth m : months) {
if (m.getYear() == year) {
l.add(m);
}
}
return l;
}

/**
* 获取农历月们
*
Expand Down
40 changes: 24 additions & 16 deletions src/main/java/com/nlf/calendar/Solar.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public Solar(int year, int month, int day, int hour, int minute, int second) {
if (day < 1 || day > 31) {
throw new IllegalArgumentException(String.format("wrong day %d", day));
}
if (hour < 0 || hour > 23) {
throw new IllegalArgumentException(String.format("wrong hour %d", hour));
}
if (minute < 0 || minute > 59) {
throw new IllegalArgumentException(String.format("wrong minute %d", minute));
}
Expand Down Expand Up @@ -296,31 +299,37 @@ public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String
offsetYear += 60;
}
int startYear = today.getYear() - offsetYear - 1;
while (true) {
int minYear = baseYear - 2;
while (startYear >= minYear) {
years.add(startYear);
startYear -= 60;
if (startYear < baseYear) {
years.add(baseYear);
break;
}
}
int hour = 0;
List<Integer> hours = new ArrayList<Integer>(2);
String timeZhi = timeGanZhi.substring(1);
for(int i = 0, j = LunarUtil.ZHI.length; i < j; i++){
if(LunarUtil.ZHI[i].equals(timeZhi)){
hour = (i - 1) * 2;
hours.add((i - 1) * 2);
}
}
for (Integer y : years) {
inner: for (int x = 0; x < 3; x++) {
int year = y + x;
Solar solar = fromYmdHms(year, 1, 1, hour, 0, 0);
while (solar.getYear() == year) {
if ("子".equals(timeZhi)) {
hours.add(23);
}
for (int hour: hours) {
for (Integer y : years) {
int maxYear = y + 3;
int year = y;
int month = 11;
if (year < baseYear) {
year = baseYear;
month = 1;
}
Solar solar = fromYmdHms(year, month, 1, hour, 0, 0);
while (solar.getYear() <= maxYear) {
Lunar lunar = solar.getLunar();
String 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);
break inner;
break;
}
solar = solar.next(1);
}
Expand Down Expand Up @@ -722,8 +731,7 @@ public Solar nextYear(int years) {
* @return 阳历
*/
public Solar nextMonth(int months) {
SolarMonth month = SolarMonth.fromYm(year, this.month);
month = month.next(months);
SolarMonth month = SolarMonth.fromYm(year, this.month).next(months);
int y = month.getYear();
int m = month.getMonth();
int d = day;
Expand Down Expand Up @@ -803,7 +811,7 @@ public Solar next(int days, boolean onlyWorkday) {
Solar solar = fromYmdHms(year, month, day, hour, minute, second);
if (days != 0) {
int rest = Math.abs(days);
int add = days < 1 ? -1 : 1;
int add = days < 0 ? -1 : 1;
while (rest > 0) {
solar = solar.next(add);
boolean work = true;
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/nlf/calendar/SolarWeek.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,8 @@ public SolarWeek next(int weeks, boolean separateMonth) {
week = new SolarWeek(solar.getYear(), solar.getMonth(), solar.getDay(), start);
}
} else {
int size = SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start);
if (size == index) {
Solar firstDay = week.getFirstDay();
Solar lastDay = firstDay.next(6);
if (SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start) == index) {
Solar lastDay = week.getFirstDay().next(6);
week = new SolarWeek(lastDay.getYear(), lastDay.getMonth(), lastDay.getDay(), start);
weekMonth = week.getMonth();
} else {
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/com/nlf/calendar/util/HolidayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,24 @@ private static String findBackward(String key){
private static List<Holiday> findHolidaysForward(String key){
List<Holiday> l = new ArrayList<Holiday>();
String s = findForward(key);
if(null==s) {
return l;
}
while(s.startsWith(key)){
l.add(buildHolidayForward(s));
s = s.substring(SIZE);
if(null!=s) {
while (s.startsWith(key)) {
l.add(buildHolidayForward(s));
s = s.substring(SIZE);
}
}
return l;
}

private static List<Holiday> findHolidaysBackward(String key){
List<Holiday> l = new ArrayList<Holiday>();
String s = findBackward(key);
if(null==s) {
return l;
}
while(s.endsWith(key)){
l.add(buildHolidayBackward(s));
s = s.substring(0,s.length()-SIZE);
if(null!=s) {
while (s.endsWith(key)) {
l.add(0, buildHolidayBackward(s));
s = s.substring(0, s.length() - SIZE);
}
}
Collections.reverse(l);
return l;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/nlf/calendar/util/LunarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class LunarUtil{
public static final String[] WU_HOU = {"蚯蚓结","麋角解","水泉动","雁北乡","鹊始巢","雉始雊","鸡始乳","征鸟厉疾","水泽腹坚","东风解冻","蛰虫始振","鱼陟负冰","獭祭鱼","候雁北","草木萌动","桃始华","仓庚鸣","鹰化为鸠","玄鸟至","雷乃发声","始电","桐始华","田鼠化为鴽","虹始见","萍始生","鸣鸠拂奇羽","戴胜降于桑","蝼蝈鸣","蚯蚓出","王瓜生","苦菜秀","靡草死","麦秋至","螳螂生","鵙始鸣","反舌无声","鹿角解","蜩始鸣","半夏生","温风至","蟋蟀居壁","鹰始挚","腐草为萤","土润溽暑","大雨行时","凉风至","白露降","寒蝉鸣","鹰乃祭鸟","天地始肃","禾乃登","鸿雁来","玄鸟归","群鸟养羞","雷始收声","蛰虫坯户","水始涸","鸿雁来宾","雀入大水为蛤","菊有黄花","豺乃祭兽","草木黄落","蛰虫咸俯","水始冰","地始冻","雉入大水为蜃","虹藏不见","天气上升地气下降","闭塞而成冬","鹖鴠不鸣","虎始交","荔挺出"};
/** 天干 */
public static final String[] GAN = {"","甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
/** 喜神方位,《喜神方位歌》:甲己在艮乙庚乾,丙辛坤位喜神安.  丁壬只在离宫坐,戊癸原在在巽间。 */
/** 喜神方位,《喜神方位歌》:甲己在艮乙庚乾,丙辛坤位喜神安丁壬只在离宫坐,戊癸原在在巽间。 */
public static final String[] POSITION_XI = {"","艮","乾","坤","离","巽","艮","乾","坤","离","巽"};
/** 阳贵方位,《阳贵神歌》:甲戊坤艮位,乙己是坤坎,庚辛居离艮,丙丁兑与乾,震巽属何日,壬癸贵神安。 */
public static final String[] POSITION_YANG_GUI = {"","坤","坤","兑","乾","艮","坎","离","艮","震","巽"};
Expand All @@ -33,7 +33,7 @@ public class LunarUtil{
public static final String[] POSITION_FU = {"","巽","巽","震","震","坎","离","坤","坤","乾","兑"};
/** 流派2《福神方位歌》:甲己正北是福神,丙辛西北乾宫存,乙庚坤位戊癸艮,丁壬巽上好追寻。 */
public static final String[] POSITION_FU_2 = {"","坎","坤","乾","巽","艮","坎","坤","乾","巽","艮"};
/** 财神方位,《财神方位歌》:甲乙东北是财神,丙丁向在西南寻,戊己正北坐方位,庚辛正东去安身,壬癸原来正南坐,便是财神方位真。 */
/** 财神方位,《财神方位歌》:甲乙东北是财神,丙丁向在西南寻,戊己正北坐方位,庚辛正东去安身,壬癸原来正南坐,便是财神方位真。 */
public static final String[] POSITION_CAI = {"","艮","艮","坤","坤","坎","坎","震","震","离","离"};
/** 年太岁方位 */
public static final String[] POSITION_TAI_SUI_YEAR = {"坎","艮","艮","震","巽","巽","离","坤","坤","兑","坎","坎"};
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/test/BaZiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,45 @@ public void test13(){
Assert.assertEquals("日柱", "戊子", eightChar.getDay());
Assert.assertEquals("时柱", "甲寅", eightChar.getTime());
}

@Test
public void test14() {
List<Solar> l = Solar.fromBaZi("癸卯","甲寅","癸丑","甲子", 2, 1843);
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}

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

@Test
public void test15() {
List<Solar> l = Solar.fromBaZi("己亥","丁丑","壬寅","戊申");
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}

List<String> expected = new ArrayList<String>();
expected.add("1960-01-15 16:00:00");
expected.add("1900-01-29 16:00:00");
Assert.assertEquals(expected, actual);
}

@Test
public void test16() {
List<Solar> l = Solar.fromBaZi("己亥","丙子","癸酉","庚申");
List<String> actual = new ArrayList<String>();
for (Solar solar : l) {
actual.add(solar.toYmdHms());
}

List<String> expected = new ArrayList<String>();
expected.add("1959-12-17 16:00:00");
Assert.assertEquals(expected, actual);
}
}
Loading

0 comments on commit 586a8d3

Please sign in to comment.