Skip to content

Commit 249aa36

Browse files
committed
v1.2.20 支持获取当年第几周;支持2种流派获取起运。
1 parent 1a15531 commit 249aa36

File tree

8 files changed

+148
-26
lines changed

8 files changed

+148
-26
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.2.19</version>
19+
<version>1.2.20</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.2.19</version>
15+
<version>1.2.20</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.2.19</version>
10+
<version>1.2.20</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/EightChar.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,13 +556,24 @@ public Lunar getLunar() {
556556
}
557557

558558
/**
559-
* 获取运
559+
* 使用默认流派1获取运
560560
*
561561
* @param gender 性别:1男,0女
562562
* @return 运
563563
*/
564564
public Yun getYun(int gender) {
565-
return new Yun(this, gender);
565+
return getYun(gender, 1);
566+
}
567+
568+
/**
569+
* 获取运
570+
*
571+
* @param gender 性别:1男,0女
572+
* @param sect 流派,1按天数和时辰数计算,3天1年,1天4个月,1时辰10天;2按分钟数计算
573+
* @return 运
574+
*/
575+
public Yun getYun(int gender, int sect) {
576+
return new Yun(this, gender, sect);
566577
}
567578

568579
/**

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ public int getStart() {
155155
*
156156
* @return 周序号,从1开始
157157
*/
158-
@SuppressWarnings("MagicConstant")
159158
public int getIndex() {
160159
Calendar c = ExactDate.fromYmd(year, month, 1);
161160
int firstDayWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
@@ -166,6 +165,21 @@ public int getIndex() {
166165
return (int) Math.ceil((day + offset) / 7D);
167166
}
168167

168+
/**
169+
* 获取当前日期是在当年第几周
170+
*
171+
* @return 周序号,从1开始
172+
*/
173+
public int getIndexInYear() {
174+
Calendar c = ExactDate.fromYmd(year, 1, 1);
175+
int firstDayWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
176+
int offset = firstDayWeek - start;
177+
if(offset < 0) {
178+
offset += 7;
179+
}
180+
return (int) Math.ceil((SolarUtil.getDaysInYear(year, month, day) + offset) / 7D);
181+
}
182+
169183
/**
170184
* 周推移
171185
*
@@ -228,7 +242,6 @@ public SolarWeek next(int weeks, boolean separateMonth) {
228242
*
229243
* @return 本周第一天的阳历日期
230244
*/
231-
@SuppressWarnings("MagicConstant")
232245
public Solar getFirstDay() {
233246
Calendar c = ExactDate.fromYmd(year, month, day);
234247
int week = c.get(Calendar.DAY_OF_WEEK) - 1;
@@ -260,6 +273,7 @@ public Solar getFirstDayInMonth() {
260273
*
261274
* @return 本周的阳历日期列表
262275
*/
276+
@SuppressWarnings("all")
263277
public List<Solar> getDays() {
264278
Solar firstDay = getFirstDay();
265279
List<Solar> l = new ArrayList<Solar>();

src/main/java/com/nlf/calendar/eightchar/Yun.java

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,64 @@ public class Yun {
1515
* 性别(1男,0女)
1616
*/
1717
private int gender;
18+
1819
/**
1920
* 起运年数
2021
*/
2122
private int startYear;
23+
2224
/**
2325
* 起运月数
2426
*/
2527
private int startMonth;
28+
2629
/**
2730
* 起运天数
2831
*/
2932
private int startDay;
33+
34+
/**
35+
* 起运小时数
36+
*/
37+
private int startHour;
38+
3039
/**
3140
* 是否顺推
3241
*/
3342
private boolean forward;
43+
3444
private Lunar lunar;
3545

46+
/**
47+
* 使用默认流派1初始化运
48+
* @param eightChar 八字
49+
* @param gender 性别,1男,0女
50+
*/
3651
public Yun(EightChar eightChar, int gender) {
52+
this(eightChar, gender, 1);
53+
}
54+
55+
/**
56+
* 初始化运
57+
* @param eightChar 八字
58+
* @param gender 性别,1男,0女
59+
* @param sect 流派,1按天数和时辰数计算,3天1年,1天4个月,1时辰10天;2按分钟数计算
60+
*/
61+
public Yun(EightChar eightChar, int gender, int sect) {
3762
this.lunar = eightChar.getLunar();
3863
this.gender = gender;
3964
// 阳
4065
boolean yang = 0 == lunar.getYearGanIndexExact() % 2;
4166
// 男
4267
boolean man = 1 == gender;
4368
forward = (yang && man) || (!yang && !man);
44-
computeStart();
69+
computeStart(sect);
4570
}
4671

4772
/**
4873
* 起运计算
4974
*/
50-
@SuppressWarnings("MagicConstant")
51-
private void computeStart() {
75+
private void computeStart(int sect) {
5276
// 上节
5377
JieQi prev = lunar.getPrevJie();
5478
// 下节
@@ -58,24 +82,46 @@ private void computeStart() {
5882
// 阳男阴女顺推,阴男阳女逆推
5983
Solar start = forward ? current : prev.getSolar();
6084
Solar end = forward ? next.getSolar() : current;
61-
int endTimeZhiIndex = (end.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().substring(11, 16));
62-
int startTimeZhiIndex = (start.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substring(11, 16));
63-
// 时辰差
64-
int hourDiff = endTimeZhiIndex - startTimeZhiIndex;
65-
// 天数差
66-
int dayDiff = ExactDate.getDaysBetween(start.getYear(), start.getMonth(), start.getDay(), end.getYear(), end.getMonth(), end.getDay());
67-
if (hourDiff < 0) {
68-
hourDiff += 12;
69-
dayDiff--;
85+
86+
int year;
87+
int month;
88+
int day;
89+
int hour = 0;
90+
91+
if (2 == sect) {
92+
long minutes = (end.getCalendar().getTimeInMillis() - start.getCalendar().getTimeInMillis()) / 60000;
93+
long y = minutes / 4320;
94+
minutes -= y * 4320;
95+
long m = minutes / 360;
96+
minutes -= m * 360;
97+
long d = minutes / 12;
98+
minutes -= d * 12;
99+
long h = minutes * 2;
100+
year = (int)y;
101+
month = (int)m;
102+
day = (int)d;
103+
hour = (int)h;
104+
} else {
105+
int endTimeZhiIndex = (end.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().substring(11, 16));
106+
int startTimeZhiIndex = (start.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substring(11, 16));
107+
// 时辰差
108+
int hourDiff = endTimeZhiIndex - startTimeZhiIndex;
109+
// 天数差
110+
int dayDiff = ExactDate.getDaysBetween(start.getYear(), start.getMonth(), start.getDay(), end.getYear(), end.getMonth(), end.getDay());
111+
if (hourDiff < 0) {
112+
hourDiff += 12;
113+
dayDiff--;
114+
}
115+
int monthDiff = hourDiff * 10 / 30;
116+
month = dayDiff * 4 + monthDiff;
117+
day = hourDiff * 10 - monthDiff * 30;
118+
year = month / 12;
119+
month = month - year * 12;
70120
}
71-
int monthDiff = hourDiff * 10 / 30;
72-
int month = dayDiff * 4 + monthDiff;
73-
int day = hourDiff * 10 - monthDiff * 30;
74-
int year = month / 12;
75-
month = month - year * 12;
76121
this.startYear = year;
77122
this.startMonth = month;
78123
this.startDay = day;
124+
this.startHour = hour;
79125
}
80126

81127
/**
@@ -114,6 +160,15 @@ public int getStartDay() {
114160
return startDay;
115161
}
116162

163+
/**
164+
* 获取起运小时数
165+
*
166+
* @return 起运小时数
167+
*/
168+
public int getStartHour() {
169+
return startHour;
170+
}
171+
117172
/**
118173
* 是否顺推
119174
*
@@ -132,13 +187,13 @@ public Lunar getLunar() {
132187
*
133188
* @return 阳历日期
134189
*/
135-
@SuppressWarnings("MagicConstant")
136190
public Solar getStartSolar() {
137191
Solar birth = lunar.getSolar();
138-
Calendar c = ExactDate.fromYmd(birth.getYear(), birth.getMonth(), birth.getDay());
192+
Calendar c = ExactDate.fromYmdHms(birth.getYear(), birth.getMonth(), birth.getDay(), birth.getHour(), birth.getMinute(), birth.getSecond());
139193
c.add(Calendar.YEAR, startYear);
140194
c.add(Calendar.MONTH, startMonth);
141195
c.add(Calendar.DATE, startDay);
196+
c.add(Calendar.HOUR, startHour);
142197
return Solar.fromCalendar(c);
143198
}
144199

src/test/java/test/SolarWeekTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,16 @@ public void test9() {
7171
Assert.assertEquals(2, week.getIndex());
7272
}
7373

74+
@Test
75+
public void test10() {
76+
SolarWeek week = SolarWeek.fromYmd(2022, 3, 6, 0);
77+
Assert.assertEquals(11, week.getIndexInYear());
78+
}
79+
80+
@Test
81+
public void test11() {
82+
SolarWeek week = SolarWeek.fromYmd(2022, 3, 6, 1);
83+
Assert.assertEquals(10, week.getIndexInYear());
84+
}
85+
7486
}

src/test/java/test/YunTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,34 @@ public void test3() {
5353
Assert.assertEquals("起运阳历", "2020-02-06", yun.getStartSolar().toYmd());
5454
}
5555

56+
@Test
57+
public void test4() {
58+
Solar solar = new Solar(2022, 3, 9, 20, 51, 0);
59+
Lunar lunar = solar.getLunar();
60+
EightChar eightChar = lunar.getEightChar();
61+
Yun yun = eightChar.getYun(1);
62+
Assert.assertEquals("起运阳历", "2030-12-19", yun.getStartSolar().toYmd());
63+
}
64+
65+
@Test
66+
public void test5() {
67+
Solar solar = new Solar(2022, 3, 9, 20, 51, 0);
68+
Lunar lunar = solar.getLunar();
69+
EightChar eightChar = lunar.getEightChar();
70+
Yun yun = eightChar.getYun(1, 2);
71+
Assert.assertEquals("起运年数", 8, yun.getStartYear());
72+
Assert.assertEquals("起运月数", 9, yun.getStartMonth());
73+
Assert.assertEquals("起运天数", 2, yun.getStartDay());
74+
Assert.assertEquals("起运阳历", "2030-12-12", yun.getStartSolar().toYmd());
75+
}
76+
77+
@Test
78+
public void test6() {
79+
Solar solar = new Solar(2018, 6, 11, 9, 30, 0);
80+
Lunar lunar = solar.getLunar();
81+
EightChar eightChar = lunar.getEightChar();
82+
Yun yun = eightChar.getYun(0, 2);
83+
Assert.assertEquals("起运阳历", "2020-03-21", yun.getStartSolar().toYmd());
84+
}
85+
5686
}

0 commit comments

Comments
 (0)