diff --git a/README.md b/README.md
index 885851b..52779af 100644
--- a/README.md
+++ b/README.md
@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
cn.6tail
lunar
- 1.2.19
+ 1.2.20
```
diff --git a/README_EN.md b/README_EN.md
index edfcfa6..d361d8f 100644
--- a/README_EN.md
+++ b/README_EN.md
@@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
cn.6tail
lunar
- 1.2.19
+ 1.2.20
```
diff --git a/pom.xml b/pom.xml
index 5a9f981..34388a8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,7 +7,7 @@
cn.6tail
lunar
jar
- 1.2.19
+ 1.2.20
${project.groupId}:${project.artifactId}
https://github.com/6tail/lunar-java
a calendar library for Solar and Chinese Lunar
diff --git a/src/main/java/com/nlf/calendar/EightChar.java b/src/main/java/com/nlf/calendar/EightChar.java
index 57bd057..a02d92d 100644
--- a/src/main/java/com/nlf/calendar/EightChar.java
+++ b/src/main/java/com/nlf/calendar/EightChar.java
@@ -556,13 +556,24 @@ public class EightChar {
}
/**
- * 获取运
+ * 使用默认流派1获取运
*
* @param gender 性别:1男,0女
* @return 运
*/
public Yun getYun(int gender) {
- return new Yun(this, gender);
+ return getYun(gender, 1);
+ }
+
+ /**
+ * 获取运
+ *
+ * @param gender 性别:1男,0女
+ * @param sect 流派,1按天数和时辰数计算,3天1年,1天4个月,1时辰10天;2按分钟数计算
+ * @return 运
+ */
+ public Yun getYun(int gender, int sect) {
+ return new Yun(this, gender, sect);
}
/**
diff --git a/src/main/java/com/nlf/calendar/SolarWeek.java b/src/main/java/com/nlf/calendar/SolarWeek.java
index 991d72b..8aacc65 100644
--- a/src/main/java/com/nlf/calendar/SolarWeek.java
+++ b/src/main/java/com/nlf/calendar/SolarWeek.java
@@ -155,7 +155,6 @@ public class SolarWeek {
*
* @return 周序号,从1开始
*/
- @SuppressWarnings("MagicConstant")
public int getIndex() {
Calendar c = ExactDate.fromYmd(year, month, 1);
int firstDayWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
@@ -166,6 +165,21 @@ public class SolarWeek {
return (int) Math.ceil((day + offset) / 7D);
}
+ /**
+ * 获取当前日期是在当年第几周
+ *
+ * @return 周序号,从1开始
+ */
+ public int getIndexInYear() {
+ Calendar c = ExactDate.fromYmd(year, 1, 1);
+ int firstDayWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
+ int offset = firstDayWeek - start;
+ if(offset < 0) {
+ offset += 7;
+ }
+ return (int) Math.ceil((SolarUtil.getDaysInYear(year, month, day) + offset) / 7D);
+ }
+
/**
* 周推移
*
@@ -228,7 +242,6 @@ public class SolarWeek {
*
* @return 本周第一天的阳历日期
*/
- @SuppressWarnings("MagicConstant")
public Solar getFirstDay() {
Calendar c = ExactDate.fromYmd(year, month, day);
int week = c.get(Calendar.DAY_OF_WEEK) - 1;
@@ -260,6 +273,7 @@ public class SolarWeek {
*
* @return 本周的阳历日期列表
*/
+ @SuppressWarnings("all")
public List getDays() {
Solar firstDay = getFirstDay();
List l = new ArrayList();
diff --git a/src/main/java/com/nlf/calendar/eightchar/Yun.java b/src/main/java/com/nlf/calendar/eightchar/Yun.java
index 5a39444..12e4887 100644
--- a/src/main/java/com/nlf/calendar/eightchar/Yun.java
+++ b/src/main/java/com/nlf/calendar/eightchar/Yun.java
@@ -15,25 +15,50 @@ public class Yun {
* 性别(1男,0女)
*/
private int gender;
+
/**
* 起运年数
*/
private int startYear;
+
/**
* 起运月数
*/
private int startMonth;
+
/**
* 起运天数
*/
private int startDay;
+
+ /**
+ * 起运小时数
+ */
+ private int startHour;
+
/**
* 是否顺推
*/
private boolean forward;
+
private Lunar lunar;
+ /**
+ * 使用默认流派1初始化运
+ * @param eightChar 八字
+ * @param gender 性别,1男,0女
+ */
public Yun(EightChar eightChar, int gender) {
+ this(eightChar, gender, 1);
+ }
+
+ /**
+ * 初始化运
+ * @param eightChar 八字
+ * @param gender 性别,1男,0女
+ * @param sect 流派,1按天数和时辰数计算,3天1年,1天4个月,1时辰10天;2按分钟数计算
+ */
+ public Yun(EightChar eightChar, int gender, int sect) {
this.lunar = eightChar.getLunar();
this.gender = gender;
// 阳
@@ -41,14 +66,13 @@ public class Yun {
// 男
boolean man = 1 == gender;
forward = (yang && man) || (!yang && !man);
- computeStart();
+ computeStart(sect);
}
/**
* 起运计算
*/
- @SuppressWarnings("MagicConstant")
- private void computeStart() {
+ private void computeStart(int sect) {
// 上节
JieQi prev = lunar.getPrevJie();
// 下节
@@ -58,24 +82,46 @@ public class Yun {
// 阳男阴女顺推,阴男阳女逆推
Solar start = forward ? current : prev.getSolar();
Solar end = forward ? next.getSolar() : current;
- int endTimeZhiIndex = (end.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().substring(11, 16));
- int startTimeZhiIndex = (start.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substring(11, 16));
- // 时辰差
- int hourDiff = endTimeZhiIndex - startTimeZhiIndex;
- // 天数差
- int dayDiff = ExactDate.getDaysBetween(start.getYear(), start.getMonth(), start.getDay(), end.getYear(), end.getMonth(), end.getDay());
- if (hourDiff < 0) {
- hourDiff += 12;
- dayDiff--;
+
+ int year;
+ int month;
+ int day;
+ int hour = 0;
+
+ if (2 == sect) {
+ long minutes = (end.getCalendar().getTimeInMillis() - start.getCalendar().getTimeInMillis()) / 60000;
+ long y = minutes / 4320;
+ minutes -= y * 4320;
+ long m = minutes / 360;
+ minutes -= m * 360;
+ long d = minutes / 12;
+ minutes -= d * 12;
+ long h = minutes * 2;
+ year = (int)y;
+ month = (int)m;
+ day = (int)d;
+ hour = (int)h;
+ } else {
+ int endTimeZhiIndex = (end.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().substring(11, 16));
+ int startTimeZhiIndex = (start.getHour() == 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substring(11, 16));
+ // 时辰差
+ int hourDiff = endTimeZhiIndex - startTimeZhiIndex;
+ // 天数差
+ int dayDiff = ExactDate.getDaysBetween(start.getYear(), start.getMonth(), start.getDay(), end.getYear(), end.getMonth(), end.getDay());
+ if (hourDiff < 0) {
+ hourDiff += 12;
+ dayDiff--;
+ }
+ int monthDiff = hourDiff * 10 / 30;
+ month = dayDiff * 4 + monthDiff;
+ day = hourDiff * 10 - monthDiff * 30;
+ year = month / 12;
+ month = month - year * 12;
}
- int monthDiff = hourDiff * 10 / 30;
- int month = dayDiff * 4 + monthDiff;
- int day = hourDiff * 10 - monthDiff * 30;
- int year = month / 12;
- month = month - year * 12;
this.startYear = year;
this.startMonth = month;
this.startDay = day;
+ this.startHour = hour;
}
/**
@@ -114,6 +160,15 @@ public class Yun {
return startDay;
}
+ /**
+ * 获取起运小时数
+ *
+ * @return 起运小时数
+ */
+ public int getStartHour() {
+ return startHour;
+ }
+
/**
* 是否顺推
*
@@ -132,13 +187,13 @@ public class Yun {
*
* @return 阳历日期
*/
- @SuppressWarnings("MagicConstant")
public Solar getStartSolar() {
Solar birth = lunar.getSolar();
- Calendar c = ExactDate.fromYmd(birth.getYear(), birth.getMonth(), birth.getDay());
+ Calendar c = ExactDate.fromYmdHms(birth.getYear(), birth.getMonth(), birth.getDay(), birth.getHour(), birth.getMinute(), birth.getSecond());
c.add(Calendar.YEAR, startYear);
c.add(Calendar.MONTH, startMonth);
c.add(Calendar.DATE, startDay);
+ c.add(Calendar.HOUR, startHour);
return Solar.fromCalendar(c);
}
diff --git a/src/test/java/test/SolarWeekTest.java b/src/test/java/test/SolarWeekTest.java
index 4e11c28..b79ddf3 100644
--- a/src/test/java/test/SolarWeekTest.java
+++ b/src/test/java/test/SolarWeekTest.java
@@ -71,4 +71,16 @@ public class SolarWeekTest {
Assert.assertEquals(2, week.getIndex());
}
+ @Test
+ public void test10() {
+ SolarWeek week = SolarWeek.fromYmd(2022, 3, 6, 0);
+ Assert.assertEquals(11, week.getIndexInYear());
+ }
+
+ @Test
+ public void test11() {
+ SolarWeek week = SolarWeek.fromYmd(2022, 3, 6, 1);
+ Assert.assertEquals(10, week.getIndexInYear());
+ }
+
}
diff --git a/src/test/java/test/YunTest.java b/src/test/java/test/YunTest.java
index 8acf117..44baecf 100644
--- a/src/test/java/test/YunTest.java
+++ b/src/test/java/test/YunTest.java
@@ -53,4 +53,34 @@ public class YunTest {
Assert.assertEquals("起运阳历", "2020-02-06", yun.getStartSolar().toYmd());
}
+ @Test
+ public void test4() {
+ Solar solar = new Solar(2022, 3, 9, 20, 51, 0);
+ Lunar lunar = solar.getLunar();
+ EightChar eightChar = lunar.getEightChar();
+ Yun yun = eightChar.getYun(1);
+ Assert.assertEquals("起运阳历", "2030-12-19", yun.getStartSolar().toYmd());
+ }
+
+ @Test
+ public void test5() {
+ Solar solar = new Solar(2022, 3, 9, 20, 51, 0);
+ Lunar lunar = solar.getLunar();
+ EightChar eightChar = lunar.getEightChar();
+ Yun yun = eightChar.getYun(1, 2);
+ Assert.assertEquals("起运年数", 8, yun.getStartYear());
+ Assert.assertEquals("起运月数", 9, yun.getStartMonth());
+ Assert.assertEquals("起运天数", 2, yun.getStartDay());
+ Assert.assertEquals("起运阳历", "2030-12-12", yun.getStartSolar().toYmd());
+ }
+
+ @Test
+ public void test6() {
+ Solar solar = new Solar(2018, 6, 11, 9, 30, 0);
+ Lunar lunar = solar.getLunar();
+ EightChar eightChar = lunar.getEightChar();
+ Yun yun = eightChar.getYun(0, 2);
+ Assert.assertEquals("起运阳历", "2020-03-21", yun.getStartSolar().toYmd());
+ }
+
}