v1.2.3 修复除夕错误;代码优化。
This commit is contained in:
@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.6tail</groupId>
|
<groupId>cn.6tail</groupId>
|
||||||
<artifactId>lunar</artifactId>
|
<artifactId>lunar</artifactId>
|
||||||
<version>1.2.2</version>
|
<version>1.2.3</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.2</version>
|
<version>1.2.3</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.2</version>
|
<version>1.2.3</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>
|
||||||
|
|||||||
@@ -298,12 +298,14 @@ public class Lunar {
|
|||||||
private void computeMonth() {
|
private void computeMonth() {
|
||||||
Solar start = null;
|
Solar start = null;
|
||||||
Solar end;
|
Solar end;
|
||||||
|
String ymd = solar.toYmd();
|
||||||
|
String time = solar.toYmdHms();
|
||||||
|
int size = JIE_QI_IN_USE.length;
|
||||||
|
|
||||||
//序号:大雪以前-3,大雪到小寒之间-2,小寒到立春之间-1,立春之后0
|
//序号:大雪以前-3,大雪到小寒之间-2,小寒到立春之间-1,立春之后0
|
||||||
int index = -3;
|
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]);
|
end = jieQi.get(JIE_QI_IN_USE[i]);
|
||||||
String ymd = solar.toYmd();
|
|
||||||
String symd = null == start ? ymd : start.toYmd();
|
String symd = null == start ? ymd : start.toYmd();
|
||||||
if (ymd.compareTo(symd) >= 0 && ymd.compareTo(end.toYmd()) < 0) {
|
if (ymd.compareTo(symd) >= 0 && ymd.compareTo(end.toYmd()) < 0) {
|
||||||
break;
|
break;
|
||||||
@@ -313,27 +315,26 @@ public class Lunar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//干偏移值(以立春当天起算)
|
//干偏移值(以立春当天起算)
|
||||||
int gOffset = (((yearGanIndexByLiChun+(index<0?1:0)) % 5 + 1) * 2) % 10;
|
int offset = (((yearGanIndexByLiChun+(index<0?1:0)) % 5 + 1) * 2) % 10;
|
||||||
monthGanIndex = ((index<0?index+10:index) + gOffset) % 10;
|
monthGanIndex = ((index<0?index+10:index) + offset) % 10;
|
||||||
monthZhiIndex = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
|
monthZhiIndex = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
|
||||||
|
|
||||||
int indexExact = -3;
|
|
||||||
start = null;
|
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]);
|
end = jieQi.get(JIE_QI_IN_USE[i]);
|
||||||
String time = solar.toYmdHms();
|
|
||||||
String stime = null == start ? time : start.toYmdHms();
|
String stime = null == start ? time : start.toYmdHms();
|
||||||
if (time.compareTo(stime) >= 0 && time.compareTo(end.toYmdHms()) < 0) {
|
if (time.compareTo(stime) >= 0 && time.compareTo(end.toYmdHms()) < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
start = end;
|
start = end;
|
||||||
indexExact++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
//干偏移值(以立春交接时刻起算)
|
//干偏移值(以立春交接时刻起算)
|
||||||
int gOffsetExact = (((yearGanIndexExact+(indexExact<0?1:0)) % 5 + 1) * 2) % 10;
|
offset = (((yearGanIndexExact+(index<0?1:0)) % 5 + 1) * 2) % 10;
|
||||||
monthGanIndexExact = ((indexExact<0?indexExact+10:indexExact) + gOffsetExact) % 10;
|
monthGanIndexExact = ((index<0?index+10:index) + offset) % 10;
|
||||||
monthZhiIndexExact = ((indexExact<0?indexExact+12:indexExact) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
|
monthZhiIndexExact = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -829,16 +830,14 @@ public class Lunar {
|
|||||||
*/
|
*/
|
||||||
public String getJie() {
|
public String getJie() {
|
||||||
String jie = "";
|
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];
|
String key = JIE_QI[i];
|
||||||
Solar d = jieQi.get(key);
|
Solar d = jieQi.get(key);
|
||||||
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
|
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
|
||||||
if(i%2==1){
|
|
||||||
jie = key;
|
jie = key;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return convertJieQi(jie);
|
return convertJieQi(jie);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -849,16 +848,14 @@ public class Lunar {
|
|||||||
*/
|
*/
|
||||||
public String getQi() {
|
public String getQi() {
|
||||||
String qi = "";
|
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];
|
String key = JIE_QI[i];
|
||||||
Solar d = jieQi.get(key);
|
Solar d = jieQi.get(key);
|
||||||
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
|
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
|
||||||
if(i%2==0){
|
|
||||||
qi = key;
|
qi = key;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return convertJieQi(qi);
|
return convertJieQi(qi);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -954,6 +951,9 @@ public class Lunar {
|
|||||||
if (null != f) {
|
if (null != f) {
|
||||||
l.add(f);
|
l.add(f);
|
||||||
}
|
}
|
||||||
|
if (Math.abs(month) == 12 && day >= 29 && year != next(1).getYear()) {
|
||||||
|
l.add("除夕");
|
||||||
|
}
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -128,7 +128,6 @@ public class LunarUtil{
|
|||||||
put("8-15","中秋节");
|
put("8-15","中秋节");
|
||||||
put("9-9","重阳节");
|
put("9-9","重阳节");
|
||||||
put("12-8","腊八节");
|
put("12-8","腊八节");
|
||||||
put("12-30","除夕");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/** 农历日期对应的非正式节日,参考《寿康宝鉴》 */
|
/** 农历日期对应的非正式节日,参考《寿康宝鉴》 */
|
||||||
@@ -1186,7 +1185,8 @@ public class LunarUtil{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(matched) {
|
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) {
|
for (int i = 0, j = ys.length(); i < j; i += 2) {
|
||||||
String m = ys.substring(i, i + 2);
|
String m = ys.substring(i, i + 2);
|
||||||
l.add(YI_JI[Integer.parseInt(m,16)]);
|
l.add(YI_JI[Integer.parseInt(m,16)]);
|
||||||
|
|||||||
@@ -125,6 +125,31 @@ public class LunarTest {
|
|||||||
Assert.assertEquals("2020-01-06",lunar.getSolar().toString());
|
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
|
@Test
|
||||||
public void testNext(){
|
public void testNext(){
|
||||||
Solar solar = new Solar(2020,1,10,12,0,0);
|
Solar solar = new Solar(2020,1,10,12,0,0);
|
||||||
|
|||||||
Reference in New Issue
Block a user