Skip to content

Commit 586a8d3

Browse files
committed
v1.3.3 优化八字转阳历;修复少量农历年的月份错误;新增农历年天数和当年月份列表。
1 parent 60dde2c commit 586a8d3

File tree

12 files changed

+338
-66
lines changed

12 files changed

+338
-66
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
1616
<dependency>
1717
<groupId>cn.6tail</groupId>
1818
<artifactId>lunar</artifactId>
19-
<version>1.3.2</version>
19+
<version>1.3.3</version>
2020
</dependency>
2121
```
2222

README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
1212
<dependency>
1313
<groupId>cn.6tail</groupId>
1414
<artifactId>lunar</artifactId>
15-
<version>1.3.2</version>
15+
<version>1.3.3</version>
1616
</dependency>
1717
```
1818

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>cn.6tail</groupId>
88
<artifactId>lunar</artifactId>
99
<packaging>jar</packaging>
10-
<version>1.3.2</version>
10+
<version>1.3.3</version>
1111
<name>${project.groupId}:${project.artifactId}</name>
1212
<url>https://github.com/6tail/lunar-java</url>
1313
<description>a calendar library for Solar and Chinese Lunar</description>

src/main/java/com/nlf/calendar/Lunar.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,7 @@ public Lunar(int lunarYear, int lunarMonth, int lunarDay, int hour, int minute,
197197
public Lunar(Solar solar) {
198198
LunarYear ly = LunarYear.fromYear(solar.getYear());
199199
for (LunarMonth m : ly.getMonths()) {
200-
// 初一
201-
Solar firstDay = Solar.fromJulianDay(m.getFirstJulianDay());
202-
int days = solar.subtract(firstDay);
200+
int days = solar.subtract(Solar.fromJulianDay(m.getFirstJulianDay()));
203201
if (days < m.getDayCount()) {
204202
year = m.getYear();
205203
month = m.getMonth();
@@ -2899,8 +2897,7 @@ public ShuJiu getShuJiu() {
28992897
start = Solar.fromYmd(start.getYear(), start.getMonth(), start.getDay());
29002898
}
29012899

2902-
Solar end = Solar.fromYmd(start.getYear(), start.getMonth(), start.getDay());
2903-
end = end.next(81);
2900+
Solar end = Solar.fromYmd(start.getYear(), start.getMonth(), start.getDay()).next(81);
29042901

29052902
if (current.isBefore(start) || !current.isBefore(end)) {
29062903
return null;

src/main/java/com/nlf/calendar/LunarYear.java

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ private void compute() {
134134
// 冬至前的初一
135135
double w = ShouXingUtil.calcShuo(jq[0]);
136136
if (w > jq[0]) {
137-
w -= 29.5306;
137+
if (currentYear != 41 && currentYear != 193 && currentYear != 288 && currentYear != 345 && currentYear != 918 && currentYear != 1013) {
138+
w -= 29.5306;
139+
}
138140
}
139141
// 递推每月初一
140142
for (int i = 0, j = hs.length; i < j; i++) {
@@ -145,39 +147,41 @@ private void compute() {
145147
dayCounts[i] = (int) (hs[i + 1] - hs[i]);
146148
}
147149

148-
Integer currentYearLeap = LEAP.get(currentYear);
149-
if (null == currentYearLeap) {
150-
currentYearLeap = -1;
151-
if (hs[13] <= jq[24]) {
152-
int i = 1;
153-
while (hs[i + 1] > jq[2 * i] && i < 13) {
154-
i++;
150+
int prevYear = currentYear - 1;
151+
152+
int leapYear = -1;
153+
int leapIndex = -1;
154+
155+
Integer leap = LEAP.get(currentYear);
156+
if (null == leap) {
157+
leap = LEAP.get(prevYear);
158+
if (null == leap) {
159+
if (hs[13] <= jq[24]) {
160+
int i = 1;
161+
while (hs[i + 1] > jq[2 * i] && i < 13) {
162+
i++;
163+
}
164+
leapYear = currentYear;
165+
leapIndex = i;
155166
}
156-
currentYearLeap = i;
167+
} else {
168+
leapYear = prevYear;
169+
leapIndex = leap - 12;
157170
}
171+
} else {
172+
leapYear = currentYear;
173+
leapIndex = leap;
158174
}
159175

160-
int prevYear = currentYear - 1;
161-
Integer prevYearLeap = LEAP.get(prevYear);
162-
prevYearLeap = null == prevYearLeap ? -1 : prevYearLeap - 12;
163-
164176
int y = prevYear;
165177
int m = 11;
166178
for (int i = 0, j = dayCounts.length; i < j; i++) {
167179
int cm = m;
168-
boolean isNextLeap = false;
169-
if (y == currentYear && i == currentYearLeap) {
170-
cm = -cm;
171-
} else if (y == prevYear && i == prevYearLeap) {
180+
if (y == leapYear && i == leapIndex) {
172181
cm = -cm;
173182
}
174-
if (y == currentYear && i + 1 == currentYearLeap) {
175-
isNextLeap = true;
176-
} else if (y == prevYear && i + 1 == prevYearLeap) {
177-
isNextLeap = true;
178-
}
179183
this.months.add(new LunarMonth(y, cm, dayCounts[i], hs[i] + Solar.J2000));
180-
if (!isNextLeap) {
184+
if (y != leapYear || i + 1 != leapIndex) {
181185
m++;
182186
}
183187
if (m == 13) {
@@ -196,6 +200,35 @@ public int getYear() {
196200
return year;
197201
}
198202

203+
/**
204+
* 获取总天数
205+
* @return 天数
206+
*/
207+
public int getDayCount() {
208+
int n = 0;
209+
for (LunarMonth m : months) {
210+
if (m.getYear() == year) {
211+
n += m.getDayCount();
212+
}
213+
}
214+
return n;
215+
}
216+
217+
/**
218+
* 获取当年的农历月们
219+
*
220+
* @return 农历月们
221+
*/
222+
public List<LunarMonth> getMonthsInYear() {
223+
List<LunarMonth> l = new ArrayList<LunarMonth>();
224+
for (LunarMonth m : months) {
225+
if (m.getYear() == year) {
226+
l.add(m);
227+
}
228+
}
229+
return l;
230+
}
231+
199232
/**
200233
* 获取农历月们
201234
*

src/main/java/com/nlf/calendar/Solar.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public Solar(int year, int month, int day, int hour, int minute, int second) {
9292
if (day < 1 || day > 31) {
9393
throw new IllegalArgumentException(String.format("wrong day %d", day));
9494
}
95+
if (hour < 0 || hour > 23) {
96+
throw new IllegalArgumentException(String.format("wrong hour %d", hour));
97+
}
9598
if (minute < 0 || minute > 59) {
9699
throw new IllegalArgumentException(String.format("wrong minute %d", minute));
97100
}
@@ -296,31 +299,37 @@ public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String
296299
offsetYear += 60;
297300
}
298301
int startYear = today.getYear() - offsetYear - 1;
299-
while (true) {
302+
int minYear = baseYear - 2;
303+
while (startYear >= minYear) {
300304
years.add(startYear);
301305
startYear -= 60;
302-
if (startYear < baseYear) {
303-
years.add(baseYear);
304-
break;
305-
}
306306
}
307-
int hour = 0;
307+
List<Integer> hours = new ArrayList<Integer>(2);
308308
String timeZhi = timeGanZhi.substring(1);
309309
for(int i = 0, j = LunarUtil.ZHI.length; i < j; i++){
310310
if(LunarUtil.ZHI[i].equals(timeZhi)){
311-
hour = (i - 1) * 2;
311+
hours.add((i - 1) * 2);
312312
}
313313
}
314-
for (Integer y : years) {
315-
inner: for (int x = 0; x < 3; x++) {
316-
int year = y + x;
317-
Solar solar = fromYmdHms(year, 1, 1, hour, 0, 0);
318-
while (solar.getYear() == year) {
314+
if ("子".equals(timeZhi)) {
315+
hours.add(23);
316+
}
317+
for (int hour: hours) {
318+
for (Integer y : years) {
319+
int maxYear = y + 3;
320+
int year = y;
321+
int month = 11;
322+
if (year < baseYear) {
323+
year = baseYear;
324+
month = 1;
325+
}
326+
Solar solar = fromYmdHms(year, month, 1, hour, 0, 0);
327+
while (solar.getYear() <= maxYear) {
319328
Lunar lunar = solar.getLunar();
320329
String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
321330
if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi) && dgz.equals(dayGanZhi) && lunar.getTimeInGanZhi().equals(timeGanZhi)) {
322331
l.add(solar);
323-
break inner;
332+
break;
324333
}
325334
solar = solar.next(1);
326335
}
@@ -722,8 +731,7 @@ public Solar nextYear(int years) {
722731
* @return 阳历
723732
*/
724733
public Solar nextMonth(int months) {
725-
SolarMonth month = SolarMonth.fromYm(year, this.month);
726-
month = month.next(months);
734+
SolarMonth month = SolarMonth.fromYm(year, this.month).next(months);
727735
int y = month.getYear();
728736
int m = month.getMonth();
729737
int d = day;
@@ -803,7 +811,7 @@ public Solar next(int days, boolean onlyWorkday) {
803811
Solar solar = fromYmdHms(year, month, day, hour, minute, second);
804812
if (days != 0) {
805813
int rest = Math.abs(days);
806-
int add = days < 1 ? -1 : 1;
814+
int add = days < 0 ? -1 : 1;
807815
while (rest > 0) {
808816
solar = solar.next(add);
809817
boolean work = true;

src/main/java/com/nlf/calendar/SolarWeek.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,8 @@ public SolarWeek next(int weeks, boolean separateMonth) {
214214
week = new SolarWeek(solar.getYear(), solar.getMonth(), solar.getDay(), start);
215215
}
216216
} else {
217-
int size = SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start);
218-
if (size == index) {
219-
Solar firstDay = week.getFirstDay();
220-
Solar lastDay = firstDay.next(6);
217+
if (SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start) == index) {
218+
Solar lastDay = week.getFirstDay().next(6);
221219
week = new SolarWeek(lastDay.getYear(), lastDay.getMonth(), lastDay.getDay(), start);
222220
weekMonth = week.getMonth();
223221
} else {

src/main/java/com/nlf/calendar/util/HolidayUtil.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,24 @@ private static String findBackward(String key){
9090
private static List<Holiday> findHolidaysForward(String key){
9191
List<Holiday> l = new ArrayList<Holiday>();
9292
String s = findForward(key);
93-
if(null==s) {
94-
return l;
95-
}
96-
while(s.startsWith(key)){
97-
l.add(buildHolidayForward(s));
98-
s = s.substring(SIZE);
93+
if(null!=s) {
94+
while (s.startsWith(key)) {
95+
l.add(buildHolidayForward(s));
96+
s = s.substring(SIZE);
97+
}
9998
}
10099
return l;
101100
}
102101

103102
private static List<Holiday> findHolidaysBackward(String key){
104103
List<Holiday> l = new ArrayList<Holiday>();
105104
String s = findBackward(key);
106-
if(null==s) {
107-
return l;
108-
}
109-
while(s.endsWith(key)){
110-
l.add(buildHolidayBackward(s));
111-
s = s.substring(0,s.length()-SIZE);
105+
if(null!=s) {
106+
while (s.endsWith(key)) {
107+
l.add(0, buildHolidayBackward(s));
108+
s = s.substring(0, s.length() - SIZE);
109+
}
112110
}
113-
Collections.reverse(l);
114111
return l;
115112
}
116113

src/main/java/com/nlf/calendar/util/LunarUtil.java

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

src/test/java/test/BaZiTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,45 @@ public void test13(){
365365
Assert.assertEquals("日柱", "戊子", eightChar.getDay());
366366
Assert.assertEquals("时柱", "甲寅", eightChar.getTime());
367367
}
368+
369+
@Test
370+
public void test14() {
371+
List<Solar> l = Solar.fromBaZi("癸卯","甲寅","癸丑","甲子", 2, 1843);
372+
List<String> actual = new ArrayList<String>();
373+
for (Solar solar : l) {
374+
actual.add(solar.toYmdHms());
375+
}
376+
377+
List<String> expected = new ArrayList<String>();
378+
expected.add("2023-02-24 23:00:00");
379+
expected.add("1843-02-08 23:00:00");
380+
Assert.assertEquals(expected, actual);
381+
}
382+
383+
@Test
384+
public void test15() {
385+
List<Solar> l = Solar.fromBaZi("己亥","丁丑","壬寅","戊申");
386+
List<String> actual = new ArrayList<String>();
387+
for (Solar solar : l) {
388+
actual.add(solar.toYmdHms());
389+
}
390+
391+
List<String> expected = new ArrayList<String>();
392+
expected.add("1960-01-15 16:00:00");
393+
expected.add("1900-01-29 16:00:00");
394+
Assert.assertEquals(expected, actual);
395+
}
396+
397+
@Test
398+
public void test16() {
399+
List<Solar> l = Solar.fromBaZi("己亥","丙子","癸酉","庚申");
400+
List<String> actual = new ArrayList<String>();
401+
for (Solar solar : l) {
402+
actual.add(solar.toYmdHms());
403+
}
404+
405+
List<String> expected = new ArrayList<String>();
406+
expected.add("1959-12-17 16:00:00");
407+
Assert.assertEquals(expected, actual);
408+
}
368409
}

0 commit comments

Comments
 (0)