Skip to content

Commit

Permalink
v1.2.5 修复公元0至4年转阴历错误的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Sep 17, 2021
1 parent 0126490 commit 2694429
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 7 deletions.
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.2.4</version>
<version>1.2.5</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.2.4</version>
<version>1.2.5</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.2.4</version>
<version>1.2.5</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
14 changes: 13 additions & 1 deletion src/main/java/com/nlf/calendar/Lunar.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Lunar {
/**
* 实际的节气表
*/
public static final String[] JIE_QI_IN_USE = {"DA_XUE", "冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "DONG_ZHI", "XIAO_HAN", "DA_HAN", "LI_CHUN"};
public static final String[] JIE_QI_IN_USE = {"DA_XUE", "冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "DONG_ZHI", "XIAO_HAN", "DA_HAN", "LI_CHUN", "YU_SHUI", "JING_ZHE"};

/**
* 农历年
Expand Down Expand Up @@ -242,6 +242,14 @@ private void computeYear() {
yearGanIndex = offset % 10;
yearZhiIndex = offset % 12;

if (yearGanIndex < 0) {
yearGanIndex += 10;
}

if (yearZhiIndex < 0) {
yearZhiIndex += 12;
}

//以立春作为新一年的开始的干支纪年
int g = yearGanIndex;
int z = yearZhiIndex;
Expand Down Expand Up @@ -819,6 +827,10 @@ protected String convertJieQi(String name){
jq = "立春";
}else if("DA_XUE".equals(jq)){
jq = "大雪";
} else if ("YU_SHUI".equals(jq)) {
jq = "雨水";
} else if ("JING_ZHE".equals(jq)) {
jq = "惊蛰";
}
return jq;
}
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/nlf/calendar/LunarYear.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class LunarYear {

private static final Map<Integer, Integer> LEAP = new HashMap<Integer, Integer>();

private static final Map<Integer, LunarYear> CACHE = new HashMap<Integer, LunarYear>();

static {
for (int y : LEAP_11) {
LEAP.put(y, 13);
Expand Down Expand Up @@ -66,12 +68,17 @@ public LunarYear(int lunarYear) {
* @return 农历年
*/
public static LunarYear fromYear(int lunarYear) {
return new LunarYear(lunarYear);
LunarYear obj = CACHE.get(lunarYear);
if (null == obj) {
obj = new LunarYear(lunarYear);
CACHE.put(lunarYear, obj);
}
return obj;
}

private void compute() {
// 节气(中午12点)
double[] jq = new double[25];
double[] jq = new double[27];
// 合朔,即每月初一(中午12点)
double[] hs = new double[16];
// 每月天数
Expand All @@ -87,7 +94,7 @@ private void compute() {
t += ShouXingUtil.ONE_THIRD - ShouXingUtil.dtT(t);
jieQiJulianDays.add(t + Solar.J2000);
// 按中午12点算的节气
if (i > 0 && i < 26) {
if (i > 0 && i < 28) {
jq[i - 1] = Math.round(t);
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/test/BaZiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,22 @@ public void testBaZi2Solar() {
}
}

@Test
public void test4() {
Lunar lunar = Lunar.fromYmd(1985, 12, 27);
Assert.assertEquals("1995-11-05", lunar.getEightChar().getYun(1).getStartSolar().toYmd());
}

@Test
public void test5() {
Lunar lunar = Lunar.fromYmd(1985, 1, 27);
Assert.assertEquals("1989-03-28", lunar.getEightChar().getYun(1).getStartSolar().toYmd());
}

@Test
public void test6() {
Lunar lunar = Lunar.fromYmd(1986, 12, 27);
Assert.assertEquals("1990-04-15", lunar.getEightChar().getYun(1).getStartSolar().toYmd());
}

}
42 changes: 42 additions & 0 deletions src/test/java/test/LunarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,46 @@ public void test38() {
Assert.assertEquals("7013-12-24", solar.toString());
}

@Test
public void test39() {
Solar solar = Solar.fromYmd(1987, 4, 12);
Lunar lunar = solar.getLunar();
Assert.assertEquals("一九八七年三月十五", lunar.toString());
}

@Test
public void test40() {
Solar solar = Solar.fromYmd(1987, 4, 13);
Lunar lunar = solar.getLunar();
Assert.assertEquals("一九八七年三月十六", lunar.toString());
}

@Test
public void test41() {
Solar solar = Solar.fromYmd(4, 2, 10);
Lunar lunar = solar.getLunar();
Assert.assertEquals("鼠", lunar.getYearShengXiao());
}

@Test
public void test42() {
Solar solar = Solar.fromYmd(4, 2, 9);
Lunar lunar = solar.getLunar();
Assert.assertEquals("猪", lunar.getYearShengXiao());
}

@Test
public void test43() {
Solar solar = Solar.fromYmd(1, 2, 12);
Lunar lunar = solar.getLunar();
Assert.assertEquals("鸡", lunar.getYearShengXiao());
}

@Test
public void test44() {
Solar solar = Solar.fromYmd(1, 1, 1);
Lunar lunar = solar.getLunar();
Assert.assertEquals("猴", lunar.getYearShengXiao());
}

}

0 comments on commit 2694429

Please sign in to comment.