Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
249aa36176 |
@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.6tail</groupId>
|
<groupId>cn.6tail</groupId>
|
||||||
<artifactId>lunar</artifactId>
|
<artifactId>lunar</artifactId>
|
||||||
<version>1.2.19</version>
|
<version>1.2.20</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.6tail</groupId>
|
<groupId>cn.6tail</groupId>
|
||||||
<artifactId>lunar</artifactId>
|
<artifactId>lunar</artifactId>
|
||||||
<version>1.2.19</version>
|
<version>1.2.20</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -7,7 +7,7 @@
|
|||||||
<groupId>cn.6tail</groupId>
|
<groupId>cn.6tail</groupId>
|
||||||
<artifactId>lunar</artifactId>
|
<artifactId>lunar</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<version>1.2.19</version>
|
<version>1.2.20</version>
|
||||||
<name>${project.groupId}:${project.artifactId}</name>
|
<name>${project.groupId}:${project.artifactId}</name>
|
||||||
<url>https://github.com/6tail/lunar-java</url>
|
<url>https://github.com/6tail/lunar-java</url>
|
||||||
<description>a calendar library for Solar and Chinese Lunar</description>
|
<description>a calendar library for Solar and Chinese Lunar</description>
|
||||||
|
|||||||
@@ -556,13 +556,24 @@ public class EightChar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取运
|
* 使用默认流派1获取运
|
||||||
*
|
*
|
||||||
* @param gender 性别:1男,0女
|
* @param gender 性别:1男,0女
|
||||||
* @return 运
|
* @return 运
|
||||||
*/
|
*/
|
||||||
public Yun getYun(int gender) {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -155,7 +155,6 @@ public class SolarWeek {
|
|||||||
*
|
*
|
||||||
* @return 周序号,从1开始
|
* @return 周序号,从1开始
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("MagicConstant")
|
|
||||||
public int getIndex() {
|
public int getIndex() {
|
||||||
Calendar c = ExactDate.fromYmd(year, month, 1);
|
Calendar c = ExactDate.fromYmd(year, month, 1);
|
||||||
int firstDayWeek = c.get(Calendar.DAY_OF_WEEK) - 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 (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 本周第一天的阳历日期
|
* @return 本周第一天的阳历日期
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("MagicConstant")
|
|
||||||
public Solar getFirstDay() {
|
public Solar getFirstDay() {
|
||||||
Calendar c = ExactDate.fromYmd(year, month, day);
|
Calendar c = ExactDate.fromYmd(year, month, day);
|
||||||
int week = c.get(Calendar.DAY_OF_WEEK) - 1;
|
int week = c.get(Calendar.DAY_OF_WEEK) - 1;
|
||||||
@@ -260,6 +273,7 @@ public class SolarWeek {
|
|||||||
*
|
*
|
||||||
* @return 本周的阳历日期列表
|
* @return 本周的阳历日期列表
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("all")
|
||||||
public List<Solar> getDays() {
|
public List<Solar> getDays() {
|
||||||
Solar firstDay = getFirstDay();
|
Solar firstDay = getFirstDay();
|
||||||
List<Solar> l = new ArrayList<Solar>();
|
List<Solar> l = new ArrayList<Solar>();
|
||||||
|
|||||||
@@ -15,25 +15,50 @@ public class Yun {
|
|||||||
* 性别(1男,0女)
|
* 性别(1男,0女)
|
||||||
*/
|
*/
|
||||||
private int gender;
|
private int gender;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 起运年数
|
* 起运年数
|
||||||
*/
|
*/
|
||||||
private int startYear;
|
private int startYear;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 起运月数
|
* 起运月数
|
||||||
*/
|
*/
|
||||||
private int startMonth;
|
private int startMonth;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 起运天数
|
* 起运天数
|
||||||
*/
|
*/
|
||||||
private int startDay;
|
private int startDay;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 起运小时数
|
||||||
|
*/
|
||||||
|
private int startHour;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否顺推
|
* 是否顺推
|
||||||
*/
|
*/
|
||||||
private boolean forward;
|
private boolean forward;
|
||||||
|
|
||||||
private Lunar lunar;
|
private Lunar lunar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用默认流派1初始化运
|
||||||
|
* @param eightChar 八字
|
||||||
|
* @param gender 性别,1男,0女
|
||||||
|
*/
|
||||||
public Yun(EightChar eightChar, int gender) {
|
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.lunar = eightChar.getLunar();
|
||||||
this.gender = gender;
|
this.gender = gender;
|
||||||
// 阳
|
// 阳
|
||||||
@@ -41,14 +66,13 @@ public class Yun {
|
|||||||
// 男
|
// 男
|
||||||
boolean man = 1 == gender;
|
boolean man = 1 == gender;
|
||||||
forward = (yang && man) || (!yang && !man);
|
forward = (yang && man) || (!yang && !man);
|
||||||
computeStart();
|
computeStart(sect);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 起运计算
|
* 起运计算
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("MagicConstant")
|
private void computeStart(int sect) {
|
||||||
private void computeStart() {
|
|
||||||
// 上节
|
// 上节
|
||||||
JieQi prev = lunar.getPrevJie();
|
JieQi prev = lunar.getPrevJie();
|
||||||
// 下节
|
// 下节
|
||||||
@@ -58,24 +82,46 @@ public class Yun {
|
|||||||
// 阳男阴女顺推,阴男阳女逆推
|
// 阳男阴女顺推,阴男阳女逆推
|
||||||
Solar start = forward ? current : prev.getSolar();
|
Solar start = forward ? current : prev.getSolar();
|
||||||
Solar end = forward ? next.getSolar() : current;
|
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 year;
|
||||||
// 时辰差
|
int month;
|
||||||
int hourDiff = endTimeZhiIndex - startTimeZhiIndex;
|
int day;
|
||||||
// 天数差
|
int hour = 0;
|
||||||
int dayDiff = ExactDate.getDaysBetween(start.getYear(), start.getMonth(), start.getDay(), end.getYear(), end.getMonth(), end.getDay());
|
|
||||||
if (hourDiff < 0) {
|
if (2 == sect) {
|
||||||
hourDiff += 12;
|
long minutes = (end.getCalendar().getTimeInMillis() - start.getCalendar().getTimeInMillis()) / 60000;
|
||||||
dayDiff--;
|
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.startYear = year;
|
||||||
this.startMonth = month;
|
this.startMonth = month;
|
||||||
this.startDay = day;
|
this.startDay = day;
|
||||||
|
this.startHour = hour;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,6 +160,15 @@ public class Yun {
|
|||||||
return startDay;
|
return startDay;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取起运小时数
|
||||||
|
*
|
||||||
|
* @return 起运小时数
|
||||||
|
*/
|
||||||
|
public int getStartHour() {
|
||||||
|
return startHour;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否顺推
|
* 是否顺推
|
||||||
*
|
*
|
||||||
@@ -132,13 +187,13 @@ public class Yun {
|
|||||||
*
|
*
|
||||||
* @return 阳历日期
|
* @return 阳历日期
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("MagicConstant")
|
|
||||||
public Solar getStartSolar() {
|
public Solar getStartSolar() {
|
||||||
Solar birth = lunar.getSolar();
|
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.YEAR, startYear);
|
||||||
c.add(Calendar.MONTH, startMonth);
|
c.add(Calendar.MONTH, startMonth);
|
||||||
c.add(Calendar.DATE, startDay);
|
c.add(Calendar.DATE, startDay);
|
||||||
|
c.add(Calendar.HOUR, startHour);
|
||||||
return Solar.fromCalendar(c);
|
return Solar.fromCalendar(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,4 +71,16 @@ public class SolarWeekTest {
|
|||||||
Assert.assertEquals(2, week.getIndex());
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,4 +53,34 @@ public class YunTest {
|
|||||||
Assert.assertEquals("起运阳历", "2020-02-06", yun.getStartSolar().toYmd());
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user