Skip to content

Commit

Permalink
v1.2.3 修复除夕错误;代码优化。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Jun 1, 2021
1 parent dffee26 commit d65f685
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 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.2</version>
<version>1.2.3</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.2</version>
<version>1.2.3</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.2</version>
<version>1.2.3</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
42 changes: 21 additions & 21 deletions src/main/java/com/nlf/calendar/Lunar.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,14 @@ private void computeYear() {
private void computeMonth() {
Solar start = null;
Solar end;
String ymd = solar.toYmd();
String time = solar.toYmdHms();
int size = JIE_QI_IN_USE.length;

//序号:大雪以前-3,大雪到小寒之间-2,小寒到立春之间-1,立春之后0
int index = -3;
for (int i=0,j=JIE_QI_IN_USE.length;i<j;i+=2) {
for (int i=0;i<size;i+=2) {
end = jieQi.get(JIE_QI_IN_USE[i]);
String ymd = solar.toYmd();
String symd = null == start ? ymd : start.toYmd();
if (ymd.compareTo(symd) >= 0 && ymd.compareTo(end.toYmd()) < 0) {
break;
Expand All @@ -313,27 +315,26 @@ private void computeMonth() {
}

//干偏移值(以立春当天起算)
int gOffset = (((yearGanIndexByLiChun+(index<0?1:0)) % 5 + 1) * 2) % 10;
monthGanIndex = ((index<0?index+10:index) + gOffset) % 10;
int offset = (((yearGanIndexByLiChun+(index<0?1:0)) % 5 + 1) * 2) % 10;
monthGanIndex = ((index<0?index+10:index) + offset) % 10;
monthZhiIndex = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;

int indexExact = -3;
start = null;
for (int i=0,j=JIE_QI_IN_USE.length;i<j;i+=2) {
index = -3;
for (int i=0;i<size;i+=2) {
end = jieQi.get(JIE_QI_IN_USE[i]);
String time = solar.toYmdHms();
String stime = null == start ? time : start.toYmdHms();
if (time.compareTo(stime) >= 0 && time.compareTo(end.toYmdHms()) < 0) {
break;
}
start = end;
indexExact++;
index++;
}

//干偏移值(以立春交接时刻起算)
int gOffsetExact = (((yearGanIndexExact+(indexExact<0?1:0)) % 5 + 1) * 2) % 10;
monthGanIndexExact = ((indexExact<0?indexExact+10:indexExact) + gOffsetExact) % 10;
monthZhiIndexExact = ((indexExact<0?indexExact+12:indexExact) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
offset = (((yearGanIndexExact+(index<0?1:0)) % 5 + 1) * 2) % 10;
monthGanIndexExact = ((index<0?index+10:index) + offset) % 10;
monthZhiIndexExact = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
}

/**
Expand Down Expand Up @@ -829,14 +830,12 @@ protected String convertJieQi(String name){
*/
public String getJie() {
String jie = "";
for(int i=0,j=JIE_QI.length;i<j;i++){
for(int i=1,j=JIE_QI.length;i<j;i+=2){
String key = JIE_QI[i];
Solar d = jieQi.get(key);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
if(i%2==1){
jie = key;
break;
}
jie = key;
break;
}
}
return convertJieQi(jie);
Expand All @@ -849,14 +848,12 @@ public String getJie() {
*/
public String getQi() {
String qi = "";
for(int i=0,j=JIE_QI.length;i<j;i++){
for(int i=0,j=JIE_QI.length;i<j;i+=2){
String key = JIE_QI[i];
Solar d = jieQi.get(key);
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
if(i%2==0){
qi = key;
break;
}
qi = key;
break;
}
}
return convertJieQi(qi);
Expand Down Expand Up @@ -954,6 +951,9 @@ public List<String> getFestivals() {
if (null != f) {
l.add(f);
}
if (Math.abs(month) == 12 && day >= 29 && year != next(1).getYear()) {
l.add("除夕");
}
return l;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/nlf/calendar/util/LunarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public class LunarUtil{
put("8-15","中秋节");
put("9-9","重阳节");
put("12-8","腊八节");
put("12-30","除夕");
}
};
/** 农历日期对应的非正式节日,参考《寿康宝鉴》 */
Expand Down Expand Up @@ -1186,7 +1185,8 @@ public static List<String> getDayYi(String monthGanZhi,String dayGanZhi){
}
}
if(matched) {
String ys = left.substring(left.indexOf(":") + 1, left.indexOf(","));
String ys = left.substring(left.indexOf(':') + 1);
ys = ys.substring(0, ys.indexOf(','));
for (int i = 0, j = ys.length(); i < j; i += 2) {
String m = ys.substring(i, i + 2);
l.add(YI_JI[Integer.parseInt(m,16)]);
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/test/LunarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ public void test17(){
Assert.assertEquals("2020-01-06",lunar.getSolar().toString());
}

@Test
public void test18(){
Lunar lunar = new Lunar(2021,12,29);
Assert.assertEquals("除夕",lunar.getFestivals().get(0));
}

@Test
public void test19(){
Lunar lunar = new Lunar(2020,12,30);
Assert.assertEquals("除夕",lunar.getFestivals().get(0));
}

@Test
public void test20(){
Lunar lunar = new Lunar(2020,12,29);
Assert.assertEquals(0,lunar.getFestivals().size());
}

@Test
public void test21(){
Solar solar = Solar.fromYmd(2022, 1, 31);
Lunar lunar = solar.getLunar();
Assert.assertEquals("除夕",lunar.getFestivals().get(0));
}

@Test
public void testNext(){
Solar solar = new Solar(2020,1,10,12,0,0);
Expand Down

0 comments on commit d65f685

Please sign in to comment.