Skip to content

Commit

Permalink
v1.3.12 八字转阳历结果按时间先后排序,转换速度大幅提升。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Feb 6, 2024
1 parent e15a916 commit 1c3ff0c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 99 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog


## [1.3.12] - 2024-02-06
1. 八字转阳历结果按时间先后排序,转换速度大幅提升。
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.11</version>
<version>1.3.12</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.11</version>
<version>1.3.12</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.11</version>
<version>1.3.12</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
36 changes: 4 additions & 32 deletions src/main/java/com/nlf/calendar/EightChar.java
Original file line number Diff line number Diff line change
Expand Up @@ -499,22 +499,8 @@ public String getTaiXiNaYin() {
* @return 命宫
*/
public String getMingGong() {
int monthZhiIndex = 0;
int timeZhiIndex = 0;
String monthZhi = getMonthZhi();
String timeZhi = getTimeZhi();
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
if (monthZhi.equals(MONTH_ZHI[i])) {
monthZhiIndex = i;
break;
}
}
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
if (timeZhi.equals(MONTH_ZHI[i])) {
timeZhiIndex = i;
break;
}
}
int monthZhiIndex = LunarUtil.find(getMonthZhi(), MONTH_ZHI, 0);
int timeZhiIndex = LunarUtil.find(getTimeZhi(), MONTH_ZHI, 0);
int offset = monthZhiIndex + timeZhiIndex;
offset = (offset >= 14 ? 26 : 14) - offset;
int ganIndex = (lunar.getYearGanIndexExact() + 1) * 2 + offset;
Expand All @@ -539,22 +525,8 @@ public String getMingGongNaYin() {
* @return 身宫
*/
public String getShenGong() {
int monthZhiIndex = 0;
int timeZhiIndex = 0;
String monthZhi = getMonthZhi();
String timeZhi = getTimeZhi();
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
if (monthZhi.equals(MONTH_ZHI[i])) {
monthZhiIndex = i;
break;
}
}
for (int i = 0, j = MONTH_ZHI.length; i < j; i++) {
if (timeZhi.equals(LunarUtil.ZHI[i])) {
timeZhiIndex = i;
break;
}
}
int monthZhiIndex = LunarUtil.find(getMonthZhi(), MONTH_ZHI, 0);
int timeZhiIndex = LunarUtil.find(getTimeZhi(), LunarUtil.ZHI, 0);
int offset = monthZhiIndex + timeZhiIndex;
while (offset > 12) {
offset -= 12;
Expand Down
90 changes: 54 additions & 36 deletions src/main/java/com/nlf/calendar/Solar.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,49 +296,67 @@ public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String
public static List<Solar> fromBaZi(String yearGanZhi, String monthGanZhi, String dayGanZhi, String timeGanZhi, int sect, int baseYear) {
sect = (1 == sect) ? 1 : 2;
List<Solar> l = new ArrayList<Solar>();
List<Integer> years = new ArrayList<Integer>();
Solar today = fromDate(new Date());
int offsetYear = (today.getYear() - 4) % 60 - LunarUtil.getJiaZiIndex(yearGanZhi);
if(offsetYear < 0){
offsetYear += 60;
}
int startYear = today.getYear() - offsetYear - 1;
int minYear = baseYear - 2;
while (startYear >= minYear) {
years.add(startYear);
startYear -= 60;
}
List<Integer> hours = new ArrayList<Integer>(2);
String timeZhi = timeGanZhi.substring(1);
for(int i = 1, j = LunarUtil.ZHI.length; i < j; i++){
if(LunarUtil.ZHI[i].equals(timeZhi)){
hours.add((i - 1) * 2);
break;
}
// 月地支距寅月的偏移值
int m = LunarUtil.find(monthGanZhi.substring(1), LunarUtil.ZHI, -1) - 2;
if (m < 0) {
m += 12;
}
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();
// 月天干要一致
if (((LunarUtil.find(yearGanZhi.substring(0, 1), LunarUtil.GAN, -1) + 1) * 2 + m) % 10 != LunarUtil.find(monthGanZhi.substring(0,1), LunarUtil.GAN, -1)) {
return l;
}
// 1年的立春是辛酉,序号57
int y = LunarUtil.getJiaZiIndex(yearGanZhi) - 57;
if (y < 0) {
y += 60;
}
y++;
// 节令偏移值
m *= 2;
// 时辰地支转时刻,子时按零点算
int h = LunarUtil.find(timeGanZhi.substring(1), LunarUtil.ZHI, -1) * 2;
int startYear = baseYear - 1;

// 结束年
Calendar c = Calendar.getInstance(TIME_ZONE);
c.setTime(new Date());
c.set(Calendar.MILLISECOND, 0);
int endYear = c.get(Calendar.YEAR);

while (y <= endYear) {
if (y >= startYear) {
// 立春为寅月的开始
List<Solar> jieQiList = new ArrayList<Solar>(Lunar.fromYmd(y, 1, 1).getJieQiTable().values());
// 节令推移,年干支和月干支就都匹配上了
Solar solarTime = jieQiList.get(4 + m);
if (solarTime.getYear() >= baseYear) {
int mi = 0;
int s = 0;
// 日干支和节令干支的偏移值
Lunar lunar = solarTime.getLunar();
String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
int d = LunarUtil.getJiaZiIndex(dayGanZhi) - LunarUtil.getJiaZiIndex(dgz);
if (d < 0) {
d += 60;
}
if (d > 0) {
// 从节令推移天数
solarTime = solarTime.next(d);
} else if (h == solarTime.getHour()) {
// 如果正好是节令当天,且小时和节令的小时数相等的极端情况,把分钟和秒钟带上
mi = solarTime.getMinute();
s = solarTime.getSecond();
}
// 验证一下
Solar solar = Solar.fromYmdHms(solarTime.getYear(), solarTime.getMonth(), solarTime.getDay(), h, mi, s);
lunar = solar.getLunar();
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;
}
solar = solar.next(1);
}
}
y += 60;
}
return l;
}
Expand Down
28 changes: 9 additions & 19 deletions src/main/java/com/nlf/calendar/util/LunarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,13 @@ private static String hex(int n){
* @return 甲子序号
*/
public static int getJiaZiIndex(String ganZhi){
for(int i=0,j=LunarUtil.JIA_ZI.length;i<j;i++){
if(LunarUtil.JIA_ZI[i].equals(ganZhi)){
return i;
return find(ganZhi, JIA_ZI, 0);
}

public static int find(String name, String[] names, int offset) {
for(int i=0,j=names.length;i<j;i++){
if(names[i].equals(name)){
return i + offset;
}
}
return -1;
Expand Down Expand Up @@ -1020,22 +1024,8 @@ public static List<String> getTimeJi(String dayGanZhi,String timeGanZhi){
* @return 旬下标,0-5
*/
protected static int getXunIndex(String ganZhi){
String gan = ganZhi.substring(0,1);
String zhi = ganZhi.substring(1);
int ganIndex = 0;
int zhiIndex = 0;
for(int i=0,j=GAN.length;i<j;i++){
if(GAN[i].equals(gan)){
ganIndex = i;
break;
}
}
for(int i=0,j=ZHI.length;i<j;i++){
if(ZHI[i].equals(zhi)){
zhiIndex = i;
break;
}
}
int ganIndex = find(ganZhi.substring(0, 1), GAN, 0);
int zhiIndex = find(ganZhi.substring(1), ZHI, 0);
int diff = ganIndex - zhiIndex;
if(diff<0){
diff += 12;
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/test/BaZiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ public void testBaZi2Solar() {
}

List<String> expected = new ArrayList<String>();
expected.add("1976-09-21 12:00:00");
expected.add("1916-10-06 12:00:00");
expected.add("1976-09-21 12:00:00");
Assert.assertEquals(expected, actual);
}

Expand All @@ -285,8 +285,8 @@ public void testBaZi2Solar2() {
}

List<String> expected = new ArrayList<String>();
expected.add("1999-07-21 16:00:00");
expected.add("1939-08-05 16:00:00");
expected.add("1999-07-21 16:00:00");
Assert.assertEquals(expected, actual);
}

Expand All @@ -299,8 +299,8 @@ public void testBaZi2Solar3() {
}

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

Expand All @@ -313,8 +313,8 @@ public void testBaZi2Solar4() {
}

List<String> expected = new ArrayList<String>();
expected.add("2020-07-21 22:00:00");
expected.add("1960-08-05 22:00:00");
expected.add("2020-07-21 22:00:00");
Assert.assertEquals(expected, actual);
}

Expand Down Expand Up @@ -368,15 +368,15 @@ public void test13(){

@Test
public void test14() {
List<Solar> l = Solar.fromBaZi("癸卯","甲寅","癸丑","甲子", 2, 1843);
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");
expected.add("1843-02-09 00:00:00");
expected.add("2023-02-25 00:00:00");
Assert.assertEquals(expected, actual);
}

Expand All @@ -389,8 +389,8 @@ public void test15() {
}

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

Expand Down Expand Up @@ -432,8 +432,8 @@ public void test19() {
}

List<String> expected = new ArrayList<String>();
expected.add("1997-03-12 18:00:00");
expected.add("1937-03-27 18:00:00");
expected.add("1997-03-12 18:00:00");
Assert.assertEquals(expected, actual);
}

Expand Down

0 comments on commit 1c3ff0c

Please sign in to comment.