Compare commits
3 Commits
v1.2.1
...
v1.2.0-SNA
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d26313d2e3 | ||
|
|
d65f6852e9 | ||
|
|
dffee267c0 |
@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.6tail</groupId>
|
<groupId>cn.6tail</groupId>
|
||||||
<artifactId>lunar</artifactId>
|
<artifactId>lunar</artifactId>
|
||||||
<version>1.2.1</version>
|
<version>1.2.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.6tail</groupId>
|
<groupId>cn.6tail</groupId>
|
||||||
<artifactId>lunar</artifactId>
|
<artifactId>lunar</artifactId>
|
||||||
<version>1.1.0-SNAPSHOT</version>
|
<version>1.2.0-SNAPSHOT</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.1</version>
|
<version>1.2.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -34,7 +34,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.1.0-SNAPSHOT</version>
|
<version>1.2.0-SNAPSHOT</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.1</version>
|
<version>1.2.0-SNAPSHOT</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>
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.nlf.calendar;
|
package com.nlf.calendar;
|
||||||
|
|
||||||
import com.nlf.calendar.util.LunarUtil;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 节气
|
* 节气
|
||||||
*
|
*
|
||||||
@@ -48,15 +46,13 @@ public class JieQi {
|
|||||||
*/
|
*/
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
for(String key: LunarUtil.JIE){
|
for(int i=0,j=Lunar.JIE_QI.length;i<j;i++){
|
||||||
if(key.equals(name)){
|
if(name.equals(Lunar.JIE_QI[i])){
|
||||||
this.jie = true;
|
if(i%2==0){
|
||||||
return;
|
this.qi = true;
|
||||||
}
|
}else{
|
||||||
}
|
this.jie = true;
|
||||||
for(String key: LunarUtil.QI){
|
}
|
||||||
if(key.equals(name)){
|
|
||||||
this.qi = true;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,47 +11,18 @@ import java.util.*;
|
|||||||
* @author 6tail
|
* @author 6tail
|
||||||
*/
|
*/
|
||||||
public class Lunar {
|
public class Lunar {
|
||||||
/**
|
|
||||||
* 节气表头部追加农历上年末的节气名(节令:大雪),以示区分
|
|
||||||
*/
|
|
||||||
public static final String JIE_QI_PREPEND = "DA_XUE";
|
|
||||||
/**
|
|
||||||
* 节气表尾部追加农历下年初的节气名(气令:冬至),以示区分
|
|
||||||
*/
|
|
||||||
public static final String JIE_QI_APPEND = "DONG_ZHI";
|
|
||||||
/**
|
|
||||||
* 农历年初节气名(气令:冬至)
|
|
||||||
*/
|
|
||||||
public static final String JIE_QI_FIRST = "冬至";
|
|
||||||
/**
|
|
||||||
* 农历年末节气名(节令:大雪)
|
|
||||||
*/
|
|
||||||
public static final String JIE_QI_LAST = "大雪";
|
|
||||||
/**
|
|
||||||
* 节气表尾部追加阳历下年初的第一个节气名(节令:小寒),以示区分
|
|
||||||
*/
|
|
||||||
public static final String JIE_APPEND_SOLAR_FIRST = "XIAO_HAN";
|
|
||||||
/**
|
|
||||||
* 节气表尾部追加阳历下年初的第二个节气名(气令:大寒),以示区分
|
|
||||||
*/
|
|
||||||
public static final String QI_APPEND_SOLAR_SECOND = "DA_HAN";
|
|
||||||
/**
|
|
||||||
* 阳历下年初的第一个节气名(节令:小寒)
|
|
||||||
*/
|
|
||||||
public static final String JIE_SOLAR_FIRST = "小寒";
|
|
||||||
/**
|
|
||||||
* 阳历下年初的第二个节气名(气令:大寒)
|
|
||||||
*/
|
|
||||||
public static final String QI_SOLAR_SECOND = "大寒";
|
|
||||||
/**
|
/**
|
||||||
* 1天对应的毫秒
|
* 1天对应的毫秒
|
||||||
*/
|
*/
|
||||||
private static final long MS_PER_DAY = 86400000L;
|
private static final long MS_PER_DAY = 86400000L;
|
||||||
/**
|
/**
|
||||||
* 节气表,国标以冬至为首个节气
|
* 节气表,国标以冬至为首个气令
|
||||||
*/
|
*/
|
||||||
public static final String[] JIE_QI = {"冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪"};
|
public static final String[] JIE_QI = {"冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪"};
|
||||||
public static final String[] JIE_QI_IN_USE = {JIE_QI_PREPEND, "冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", JIE_QI_APPEND, JIE_APPEND_SOLAR_FIRST, QI_APPEND_SOLAR_SECOND};
|
/**
|
||||||
|
* 实际的节气表
|
||||||
|
*/
|
||||||
|
public static final String[] JIE_QI_IN_USE = {"DA_XUE", "冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "DONG_ZHI", "XIAO_HAN", "DA_HAN", "LI_CHUN"};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 农历年
|
* 农历年
|
||||||
@@ -279,16 +250,20 @@ public class Lunar {
|
|||||||
int gExact = yearGanIndex;
|
int gExact = yearGanIndex;
|
||||||
int zExact = yearZhiIndex;
|
int zExact = yearZhiIndex;
|
||||||
|
|
||||||
//获取立春的阳历时刻
|
int solarYear = solar.getYear();
|
||||||
Solar liChun = jieQi.get("立春");
|
|
||||||
|
|
||||||
String liChunYmd = liChun.toYmd();
|
|
||||||
String liChunYmdHms = liChun.toYmdHms();
|
|
||||||
String solarYmd = solar.toYmd();
|
String solarYmd = solar.toYmd();
|
||||||
String solarYmdHms = solar.toYmdHms();
|
String solarYmdHms = solar.toYmdHms();
|
||||||
|
|
||||||
|
//获取立春的阳历时刻
|
||||||
|
Solar liChun = jieQi.get("立春");
|
||||||
|
if (liChun.getYear() != solarYear) {
|
||||||
|
liChun = jieQi.get("LI_CHUN");
|
||||||
|
}
|
||||||
|
String liChunYmd = liChun.toYmd();
|
||||||
|
String liChunYmdHms = liChun.toYmdHms();
|
||||||
|
|
||||||
//阳历和阴历年份相同代表正月初一及以后
|
//阳历和阴历年份相同代表正月初一及以后
|
||||||
if (year == solar.getYear()) {
|
if (year == solarYear) {
|
||||||
//立春日期判断
|
//立春日期判断
|
||||||
if (solarYmd.compareTo(liChunYmd) < 0) {
|
if (solarYmd.compareTo(liChunYmd) < 0) {
|
||||||
g--;
|
g--;
|
||||||
@@ -299,7 +274,7 @@ public class Lunar {
|
|||||||
gExact--;
|
gExact--;
|
||||||
zExact--;
|
zExact--;
|
||||||
}
|
}
|
||||||
} else {
|
} else if (year < solarYear) {
|
||||||
if (solarYmd.compareTo(liChunYmd) >= 0) {
|
if (solarYmd.compareTo(liChunYmd) >= 0) {
|
||||||
g++;
|
g++;
|
||||||
z++;
|
z++;
|
||||||
@@ -310,91 +285,60 @@ public class Lunar {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g < 0) {
|
yearGanIndexByLiChun = (g<0?g+10:g)%10;
|
||||||
g += 10;
|
yearZhiIndexByLiChun = (z<0?z+12:z)%12;
|
||||||
}
|
|
||||||
if (g >= 10) {
|
|
||||||
g -= 10;
|
|
||||||
}
|
|
||||||
if (z < 0) {
|
|
||||||
z += 12;
|
|
||||||
}
|
|
||||||
if (z >= 12) {
|
|
||||||
z -= 12;
|
|
||||||
}
|
|
||||||
if (gExact < 0) {
|
|
||||||
gExact += 10;
|
|
||||||
}
|
|
||||||
if (gExact >= 10) {
|
|
||||||
gExact -= 10;
|
|
||||||
}
|
|
||||||
if (zExact < 0) {
|
|
||||||
zExact += 12;
|
|
||||||
}
|
|
||||||
if (zExact >= 12) {
|
|
||||||
zExact -= 12;
|
|
||||||
}
|
|
||||||
|
|
||||||
yearGanIndexByLiChun = g;
|
yearGanIndexExact = (gExact<0?gExact+10:gExact)%10;
|
||||||
yearZhiIndexByLiChun = z;
|
yearZhiIndexExact = (zExact<0?zExact+12:zExact)%12;
|
||||||
|
|
||||||
yearGanIndexExact = gExact;
|
|
||||||
yearZhiIndexExact = zExact;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 干支纪月计算
|
* 计算干支纪月
|
||||||
*/
|
*/
|
||||||
private void computeMonth() {
|
private void computeMonth() {
|
||||||
Solar start = null;
|
Solar start = null;
|
||||||
Solar end;
|
Solar end;
|
||||||
//干偏移值(以立春当天起算)
|
String ymd = solar.toYmd();
|
||||||
int gOffset = ((yearGanIndexByLiChun % 5 + 1) * 2) % 10;
|
String time = solar.toYmdHms();
|
||||||
//干偏移值(以立春交接时刻起算)
|
int size = JIE_QI_IN_USE.length;
|
||||||
int gOffsetExact = ((yearGanIndexExact % 5 + 1) * 2) % 10;
|
|
||||||
|
|
||||||
//序号:大雪到小寒之间-2,小寒到立春之间-1,立春之后0
|
//序号:大雪以前-3,大雪到小寒之间-2,小寒到立春之间-1,立春之后0
|
||||||
int index = -2;
|
int index = -3;
|
||||||
for (String jie : LunarUtil.JIE) {
|
for (int i=0;i<size;i+=2) {
|
||||||
end = jieQi.get(jie);
|
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();
|
||||||
String eymd = end.toYmd();
|
if (ymd.compareTo(symd) >= 0 && ymd.compareTo(end.toYmd()) < 0) {
|
||||||
if (ymd.compareTo(symd) >= 0 && ymd.compareTo(eymd) < 0) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
start = end;
|
start = end;
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
if (index < 0) {
|
|
||||||
index += 12;
|
|
||||||
}
|
|
||||||
|
|
||||||
monthGanIndex = (index + gOffset) % 10;
|
//干偏移值(以立春当天起算)
|
||||||
monthZhiIndex = (index + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
|
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;
|
||||||
|
|
||||||
//序号:大雪到小寒之间-2,小寒到立春之间-1,立春之后0
|
start = null;
|
||||||
int indexExact = -2;
|
index = -3;
|
||||||
for (String jie : LunarUtil.JIE) {
|
for (int i=0;i<size;i+=2) {
|
||||||
end = jieQi.get(jie);
|
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();
|
||||||
String etime = end.toYmdHms();
|
if (time.compareTo(stime) >= 0 && time.compareTo(end.toYmdHms()) < 0) {
|
||||||
if (time.compareTo(stime) >= 0 && time.compareTo(etime) < 0) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
start = end;
|
start = end;
|
||||||
indexExact++;
|
index++;
|
||||||
}
|
}
|
||||||
if (indexExact < 0) {
|
|
||||||
indexExact += 12;
|
//干偏移值(以立春交接时刻起算)
|
||||||
}
|
offset = (((yearGanIndexExact+(index<0?1:0)) % 5 + 1) * 2) % 10;
|
||||||
monthGanIndexExact = (indexExact + gOffsetExact) % 10;
|
monthGanIndexExact = ((index<0?index+10:index) + offset) % 10;
|
||||||
monthZhiIndexExact = (indexExact + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
|
monthZhiIndexExact = ((index<0?index+12:index) + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 干支纪日计算
|
* 计算干支纪日
|
||||||
*/
|
*/
|
||||||
private void computeDay() {
|
private void computeDay() {
|
||||||
Solar noon = Solar.fromYmdHms(solar.getYear(), solar.getMonth(), solar.getDay(), 12, 0, 0);
|
Solar noon = Solar.fromYmdHms(solar.getYear(), solar.getMonth(), solar.getDay(), 12, 0, 0);
|
||||||
@@ -427,7 +371,7 @@ public class Lunar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 干支纪时计算
|
* 计算干支纪时
|
||||||
*/
|
*/
|
||||||
private void computeTime() {
|
private void computeTime() {
|
||||||
String hm = (hour < 10 ? "0" : "") + hour + ":" + (minute < 10 ? "0" : "") + minute;
|
String hm = (hour < 10 ? "0" : "") + hour + ":" + (minute < 10 ? "0" : "") + minute;
|
||||||
@@ -436,7 +380,7 @@ public class Lunar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 星期计算
|
* 计算星期
|
||||||
*/
|
*/
|
||||||
private void computeWeek() {
|
private void computeWeek() {
|
||||||
this.weekIndex = solar.getWeek();
|
this.weekIndex = solar.getWeek();
|
||||||
@@ -863,29 +807,38 @@ public class Lunar {
|
|||||||
return LunarUtil.SEASON[Math.abs(month)];
|
return LunarUtil.SEASON[Math.abs(month)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected String convertJieQi(String name){
|
||||||
|
String jq = name;
|
||||||
|
if("DONG_ZHI".equals(jq)){
|
||||||
|
jq = "冬至";
|
||||||
|
}else if("DA_HAN".equals(jq)){
|
||||||
|
jq = "大寒";
|
||||||
|
}else if("XIAO_HAN".equals(jq)){
|
||||||
|
jq = "小寒";
|
||||||
|
}else if("LI_CHUN".equals(jq)){
|
||||||
|
jq = "立春";
|
||||||
|
}else if("DA_XUE".equals(jq)){
|
||||||
|
jq = "大雪";
|
||||||
|
}
|
||||||
|
return jq;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取节令
|
* 获取节令
|
||||||
*
|
*
|
||||||
* @return 节令
|
* @return 节令
|
||||||
*/
|
*/
|
||||||
public String getJie() {
|
public String getJie() {
|
||||||
for (String jie : LunarUtil.JIE) {
|
String jie = "";
|
||||||
Solar d = jieQi.get(jie);
|
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 (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
|
||||||
return jie;
|
jie = key;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 追加的节令:大雪
|
return convertJieQi(jie);
|
||||||
Solar d = jieQi.get(JIE_QI_PREPEND);
|
|
||||||
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
|
|
||||||
return JIE_QI_LAST;
|
|
||||||
}
|
|
||||||
// 追加的节令:小寒
|
|
||||||
d = jieQi.get(JIE_APPEND_SOLAR_FIRST);
|
|
||||||
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
|
|
||||||
return JIE_SOLAR_FIRST;
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -894,23 +847,16 @@ public class Lunar {
|
|||||||
* @return 气令
|
* @return 气令
|
||||||
*/
|
*/
|
||||||
public String getQi() {
|
public String getQi() {
|
||||||
for (String qi : LunarUtil.QI) {
|
String qi = "";
|
||||||
Solar d = jieQi.get(qi);
|
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 (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
|
||||||
return qi;
|
qi = key;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 追加的气令:冬至
|
return convertJieQi(qi);
|
||||||
Solar d = jieQi.get(JIE_QI_APPEND);
|
|
||||||
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
|
|
||||||
return JIE_QI_FIRST;
|
|
||||||
}
|
|
||||||
// 追加的气令:大寒
|
|
||||||
d = jieQi.get(QI_APPEND_SOLAR_SECOND);
|
|
||||||
if (d.getYear() == solar.getYear() && d.getMonth() == solar.getMonth() && d.getDay() == solar.getDay()) {
|
|
||||||
return QI_SOLAR_SECOND;
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1005,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1954,7 +1903,12 @@ public class Lunar {
|
|||||||
* @return 节气
|
* @return 节气
|
||||||
*/
|
*/
|
||||||
public JieQi getNextJie() {
|
public JieQi getNextJie() {
|
||||||
return getNearJieQi(true, LunarUtil.JIE);
|
int l = JIE_QI_IN_USE.length/2;
|
||||||
|
String[] conditions = new String[l];
|
||||||
|
for(int i=0;i<l;i++){
|
||||||
|
conditions[i] = JIE_QI_IN_USE[i*2];
|
||||||
|
}
|
||||||
|
return getNearJieQi(true, conditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1963,7 +1917,12 @@ public class Lunar {
|
|||||||
* @return 节气
|
* @return 节气
|
||||||
*/
|
*/
|
||||||
public JieQi getPrevJie() {
|
public JieQi getPrevJie() {
|
||||||
return getNearJieQi(false, LunarUtil.JIE);
|
int l = JIE_QI_IN_USE.length/2;
|
||||||
|
String[] conditions = new String[l];
|
||||||
|
for(int i=0;i<l;i++){
|
||||||
|
conditions[i] = JIE_QI_IN_USE[i*2];
|
||||||
|
}
|
||||||
|
return getNearJieQi(false, conditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1972,7 +1931,12 @@ public class Lunar {
|
|||||||
* @return 节气
|
* @return 节气
|
||||||
*/
|
*/
|
||||||
public JieQi getNextQi() {
|
public JieQi getNextQi() {
|
||||||
return getNearJieQi(true, LunarUtil.QI);
|
int l = JIE_QI_IN_USE.length/2;
|
||||||
|
String[] conditions = new String[l];
|
||||||
|
for(int i=0;i<l;i++){
|
||||||
|
conditions[i] = JIE_QI_IN_USE[i*2+1];
|
||||||
|
}
|
||||||
|
return getNearJieQi(true, conditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1981,7 +1945,12 @@ public class Lunar {
|
|||||||
* @return 节气
|
* @return 节气
|
||||||
*/
|
*/
|
||||||
public JieQi getPrevQi() {
|
public JieQi getPrevQi() {
|
||||||
return getNearJieQi(false, LunarUtil.QI);
|
int l = JIE_QI_IN_USE.length/2;
|
||||||
|
String[] conditions = new String[l];
|
||||||
|
for(int i=0;i<l;i++){
|
||||||
|
conditions[i] = JIE_QI_IN_USE[i*2+1];
|
||||||
|
}
|
||||||
|
return getNearJieQi(false, conditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2019,19 +1988,7 @@ public class Lunar {
|
|||||||
boolean filter = !filters.isEmpty();
|
boolean filter = !filters.isEmpty();
|
||||||
String today = solar.toYmdHms();
|
String today = solar.toYmdHms();
|
||||||
for (Map.Entry<String, Solar> entry : jieQi.entrySet()) {
|
for (Map.Entry<String, Solar> entry : jieQi.entrySet()) {
|
||||||
String jq = entry.getKey();
|
String jq = convertJieQi(entry.getKey());
|
||||||
if (JIE_QI_APPEND.equals(jq)) {
|
|
||||||
jq = JIE_QI_FIRST;
|
|
||||||
}
|
|
||||||
if (JIE_QI_PREPEND.equals(jq)) {
|
|
||||||
jq = JIE_QI_LAST;
|
|
||||||
}
|
|
||||||
if (JIE_APPEND_SOLAR_FIRST.equals(jq)) {
|
|
||||||
jq = JIE_SOLAR_FIRST;
|
|
||||||
}
|
|
||||||
if (QI_APPEND_SOLAR_SECOND.equals(jq)) {
|
|
||||||
jq = QI_SOLAR_SECOND;
|
|
||||||
}
|
|
||||||
if (filter) {
|
if (filter) {
|
||||||
if (!filters.contains(jq)) {
|
if (!filters.contains(jq)) {
|
||||||
continue;
|
continue;
|
||||||
@@ -2077,16 +2034,7 @@ public class Lunar {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (JIE_QI_APPEND.equals(name)) {
|
return convertJieQi(name);
|
||||||
name = JIE_QI_FIRST;
|
|
||||||
} else if (JIE_QI_PREPEND.equals(name)) {
|
|
||||||
name = JIE_QI_LAST;
|
|
||||||
} else if (JIE_APPEND_SOLAR_FIRST.equals(name)) {
|
|
||||||
name = JIE_SOLAR_FIRST;
|
|
||||||
} else if (QI_APPEND_SOLAR_SECOND.equals(name)) {
|
|
||||||
name = QI_SOLAR_SECOND;
|
|
||||||
}
|
|
||||||
return name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2528,11 +2476,11 @@ public class Lunar {
|
|||||||
@SuppressWarnings("MagicConstant")
|
@SuppressWarnings("MagicConstant")
|
||||||
public ShuJiu getShuJiu() {
|
public ShuJiu getShuJiu() {
|
||||||
Calendar currentCalendar = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay());
|
Calendar currentCalendar = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay());
|
||||||
Solar start = jieQi.get(JIE_QI_APPEND);
|
Solar start = jieQi.get("DONG_ZHI");
|
||||||
Calendar startCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay());
|
Calendar startCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay());
|
||||||
|
|
||||||
if (currentCalendar.compareTo(startCalendar) < 0) {
|
if (currentCalendar.compareTo(startCalendar) < 0) {
|
||||||
start = jieQi.get(JIE_QI_FIRST);
|
start = jieQi.get("冬至");
|
||||||
startCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay());
|
startCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,12 +51,12 @@ public class LunarYear {
|
|||||||
// 节气(中午12点)
|
// 节气(中午12点)
|
||||||
double[] jq = new double[25];
|
double[] jq = new double[25];
|
||||||
// 合朔,即每月初一(中午12点)
|
// 合朔,即每月初一(中午12点)
|
||||||
double[] hs = new double[15];
|
double[] hs = new double[16];
|
||||||
// 每月天数
|
// 每月天数
|
||||||
int[] dayCounts = new int[hs.length - 1];
|
int[] dayCounts = new int[hs.length - 1];
|
||||||
|
|
||||||
int year = this.year - 2000;
|
int year = this.year - 2000;
|
||||||
// 从上年的大雪到下年的大寒
|
// 从上年的大雪到下年的立春
|
||||||
for (int i = 0, j = Lunar.JIE_QI_IN_USE.length; i < j; i++) {
|
for (int i = 0, j = Lunar.JIE_QI_IN_USE.length; i < j; i++) {
|
||||||
// 精确的节气
|
// 精确的节气
|
||||||
double t = 36525 * ShouXingUtil.saLonT((year + (17 + i) * 15d / 360) * ShouXingUtil.PI_2);
|
double t = 36525 * ShouXingUtil.saLonT((year + (17 + i) * 15d / 360) * ShouXingUtil.PI_2);
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -112,10 +112,6 @@ public class LunarUtil{
|
|||||||
public static final String[] SEASON = {"","孟春","仲春","季春","孟夏","仲夏","季夏","孟秋","仲秋","季秋","孟冬","仲冬","季冬"};
|
public static final String[] SEASON = {"","孟春","仲春","季春","孟夏","仲夏","季夏","孟秋","仲秋","季秋","孟冬","仲冬","季冬"};
|
||||||
/** 生肖 */
|
/** 生肖 */
|
||||||
public static final String[] SHENGXIAO = {"","鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
|
public static final String[] SHENGXIAO = {"","鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
|
||||||
/** 气 */
|
|
||||||
public static final String[] QI = {"大寒","雨水","春分","谷雨","小满","夏至","大暑","处暑","秋分","霜降","小雪","冬至"};
|
|
||||||
/** 节 */
|
|
||||||
public static final String[] JIE = {"小寒","立春","惊蛰","清明","立夏","芒种","小暑","立秋","白露","寒露","立冬","大雪"};
|
|
||||||
/** 日 */
|
/** 日 */
|
||||||
public static final String[] DAY = {"","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
|
public static final String[] DAY = {"","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"};
|
||||||
/** 月相,朔月也叫新月,望月也叫满月 */
|
/** 月相,朔月也叫新月,望月也叫满月 */
|
||||||
@@ -132,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","除夕");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/** 农历日期对应的非正式节日,参考《寿康宝鉴》 */
|
/** 农历日期对应的非正式节日,参考《寿康宝鉴》 */
|
||||||
@@ -1190,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)]);
|
||||||
|
|||||||
@@ -97,7 +97,6 @@ public class SolarUtil {
|
|||||||
put("5-5", Collections.nCopies(1, "马克思诞辰纪念日"));
|
put("5-5", Collections.nCopies(1, "马克思诞辰纪念日"));
|
||||||
put("5-8", Collections.nCopies(1, "世界红十字日"));
|
put("5-8", Collections.nCopies(1, "世界红十字日"));
|
||||||
put("5-11", Collections.nCopies(1, "世界肥胖日"));
|
put("5-11", Collections.nCopies(1, "世界肥胖日"));
|
||||||
put("5-23", Collections.nCopies(1, "世界读书日"));
|
|
||||||
put("5-27", Collections.nCopies(1, "上海解放日"));
|
put("5-27", Collections.nCopies(1, "上海解放日"));
|
||||||
put("5-31", Collections.nCopies(1, "世界无烟日"));
|
put("5-31", Collections.nCopies(1, "世界无烟日"));
|
||||||
put("6-5", Collections.nCopies(1, "世界环境日"));
|
put("6-5", Collections.nCopies(1, "世界环境日"));
|
||||||
|
|||||||
@@ -57,6 +57,16 @@ public class BaZiTest {
|
|||||||
Assert.assertEquals("时柱", "辛亥", eightChar.getTime());
|
Assert.assertEquals("时柱", "辛亥", eightChar.getTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGanZhi3(){
|
||||||
|
Lunar lunar = new Lunar(2019,12,12,11,22,0);
|
||||||
|
EightChar eightChar = lunar.getEightChar();
|
||||||
|
Assert.assertEquals("年柱", "己亥", eightChar.getYear());
|
||||||
|
Assert.assertEquals("月柱", "丁丑", eightChar.getMonth());
|
||||||
|
Assert.assertEquals("日柱", "戊申", eightChar.getDay());
|
||||||
|
Assert.assertEquals("时柱", "戊午", eightChar.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHideGan() {
|
public void testHideGan() {
|
||||||
Solar solar = new Solar(2005, 12, 23, 8, 37, 0);
|
Solar solar = new Solar(2005, 12, 23, 8, 37, 0);
|
||||||
|
|||||||
@@ -73,17 +73,6 @@ public class GanZhiTest {
|
|||||||
Assert.assertEquals("丁丑",lunar.getMonthInGanZhi());
|
Assert.assertEquals("丁丑",lunar.getMonthInGanZhi());
|
||||||
Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact());
|
Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact());
|
||||||
|
|
||||||
|
|
||||||
solar = new Solar(2020,2,4,13,22,0);
|
|
||||||
lunar = solar.getLunar();
|
|
||||||
Assert.assertEquals("庚子",lunar.getYearInGanZhi());
|
|
||||||
Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun());
|
|
||||||
Assert.assertEquals("己亥",lunar.getYearInGanZhiExact());
|
|
||||||
|
|
||||||
Assert.assertEquals("戊寅",lunar.getMonthInGanZhi());
|
|
||||||
Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact());
|
|
||||||
|
|
||||||
|
|
||||||
solar = new Solar(2020,2,4,18,22,0);
|
solar = new Solar(2020,2,4,18,22,0);
|
||||||
lunar = solar.getLunar();
|
lunar = solar.getLunar();
|
||||||
Assert.assertEquals("庚子",lunar.getYearInGanZhi());
|
Assert.assertEquals("庚子",lunar.getYearInGanZhi());
|
||||||
@@ -320,4 +309,28 @@ public class GanZhiTest {
|
|||||||
Assert.assertEquals("戊辰",lunar.getYearInGanZhiExact());
|
Assert.assertEquals("戊辰",lunar.getYearInGanZhiExact());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test17() {
|
||||||
|
Solar solar = new Solar(2019, 2, 8, 13, 22, 0);
|
||||||
|
Lunar lunar = solar.getLunar();
|
||||||
|
Assert.assertEquals("己亥", lunar.getYearInGanZhi());
|
||||||
|
Assert.assertEquals("己亥", lunar.getYearInGanZhiByLiChun());
|
||||||
|
Assert.assertEquals("己亥", lunar.getYearInGanZhiExact());
|
||||||
|
|
||||||
|
Assert.assertEquals("丙寅", lunar.getMonthInGanZhi());
|
||||||
|
Assert.assertEquals("丙寅", lunar.getMonthInGanZhiExact());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test18() {
|
||||||
|
Solar solar = new Solar(2020,2,4,13,22,0);
|
||||||
|
Lunar lunar = solar.getLunar();
|
||||||
|
Assert.assertEquals("庚子",lunar.getYearInGanZhi());
|
||||||
|
Assert.assertEquals("庚子",lunar.getYearInGanZhiByLiChun());
|
||||||
|
Assert.assertEquals("己亥",lunar.getYearInGanZhiExact());
|
||||||
|
|
||||||
|
Assert.assertEquals("戊寅",lunar.getMonthInGanZhi());
|
||||||
|
Assert.assertEquals("丁丑",lunar.getMonthInGanZhiExact());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package test;
|
package test;
|
||||||
|
|
||||||
|
import com.nlf.calendar.Holiday;
|
||||||
import com.nlf.calendar.Solar;
|
import com.nlf.calendar.Solar;
|
||||||
import com.nlf.calendar.util.HolidayUtil;
|
import com.nlf.calendar.util.HolidayUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@@ -116,4 +117,12 @@ public class HolidayTest {
|
|||||||
Assert.assertEquals("2021-11-11 结婚纪念日 2021-11-11", HolidayUtil.getHoliday("2021-11-11") + "");
|
Assert.assertEquals("2021-11-11 结婚纪念日 2021-11-11", HolidayUtil.getHoliday("2021-11-11") + "");
|
||||||
Assert.assertEquals("2021-12-01 她的生日 2021-12-01", HolidayUtil.getHoliday("2021-12-01") + "");
|
Assert.assertEquals("2021-12-01 她的生日 2021-12-01", HolidayUtil.getHoliday("2021-12-01") + "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test1() {
|
||||||
|
Holiday holiday = HolidayUtil.getHoliday(2016,10,4);
|
||||||
|
Assert.assertNotNull(holiday);
|
||||||
|
Assert.assertNotNull(holiday.getTarget());
|
||||||
|
Assert.assertEquals("2016-10-01",holiday.getTarget());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,6 +64,92 @@ public class LunarTest {
|
|||||||
Assert.assertEquals("2020-05-24",lunar.getSolar().toString());
|
Assert.assertEquals("2020-05-24",lunar.getSolar().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test8(){
|
||||||
|
Lunar lunar = new Lunar(2020,12,10,13,0,0);
|
||||||
|
Assert.assertEquals("二〇二〇年腊月初十",lunar.toString());
|
||||||
|
Assert.assertEquals("2021-01-22",lunar.getSolar().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test9(){
|
||||||
|
Lunar lunar = new Lunar(1500,1,1,12,0,0);
|
||||||
|
Assert.assertEquals("1500-01-31",lunar.getSolar().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test10(){
|
||||||
|
Lunar lunar = new Lunar(1500,12,29,12,0,0);
|
||||||
|
Assert.assertEquals("1501-01-18",lunar.getSolar().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test11(){
|
||||||
|
Solar solar = new Solar(1500,1,1,12,0,0);
|
||||||
|
Assert.assertEquals("一四九九年腊月初一",solar.getLunar().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test12(){
|
||||||
|
Solar solar = new Solar(1500,12,31,12,0,0);
|
||||||
|
Assert.assertEquals("一五〇〇年腊月十一",solar.getLunar().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test13(){
|
||||||
|
Solar solar = new Solar(1582,10,4,12,0,0);
|
||||||
|
Assert.assertEquals("一五八二年九月十八",solar.getLunar().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test14(){
|
||||||
|
Solar solar = new Solar(1582,10,15,12,0,0);
|
||||||
|
Assert.assertEquals("一五八二年九月十九",solar.getLunar().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test15(){
|
||||||
|
Lunar lunar = new Lunar(1582,9,18,12,0,0);
|
||||||
|
Assert.assertEquals("1582-10-04",lunar.getSolar().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test16(){
|
||||||
|
Lunar lunar = new Lunar(1582,9,19,12,0,0);
|
||||||
|
Assert.assertEquals("1582-10-15",lunar.getSolar().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test17(){
|
||||||
|
Lunar lunar = new Lunar(2019,12,12,11,22,0);
|
||||||
|
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);
|
||||||
|
|||||||
@@ -24,4 +24,33 @@ public class YunTest {
|
|||||||
Assert.assertEquals("起运阳历", "1989-02-18", yun.getStartSolar().toYmd());
|
Assert.assertEquals("起运阳历", "1989-02-18", yun.getStartSolar().toYmd());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test2() {
|
||||||
|
Lunar lunar = new Lunar(2019, 12, 12, 11, 22, 0);
|
||||||
|
System.out.println(lunar.getMonth());
|
||||||
|
System.out.println(lunar.toFullString());
|
||||||
|
System.out.println(lunar.getSolar().toFullString());
|
||||||
|
EightChar eightChar = lunar.getEightChar();
|
||||||
|
Yun yun = eightChar.getYun(1);
|
||||||
|
Assert.assertEquals("起运年数", 0, yun.getStartYear());
|
||||||
|
Assert.assertEquals("起运月数", 1, yun.getStartMonth());
|
||||||
|
Assert.assertEquals("起运天数", 0, yun.getStartDay());
|
||||||
|
Assert.assertEquals("起运阳历", "2020-02-06", yun.getStartSolar().toYmd());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test3() {
|
||||||
|
Solar solar = new Solar(2020, 1, 6, 11, 22, 0);
|
||||||
|
Lunar lunar = solar.getLunar();
|
||||||
|
System.out.println(lunar.getMonth());
|
||||||
|
System.out.println(lunar.toFullString());
|
||||||
|
System.out.println(lunar.getSolar().toFullString());
|
||||||
|
EightChar eightChar = lunar.getEightChar();
|
||||||
|
Yun yun = eightChar.getYun(1);
|
||||||
|
Assert.assertEquals("起运年数", 0, yun.getStartYear());
|
||||||
|
Assert.assertEquals("起运月数", 1, yun.getStartMonth());
|
||||||
|
Assert.assertEquals("起运天数", 0, yun.getStartDay());
|
||||||
|
Assert.assertEquals("起运阳历", "2020-02-06", yun.getStartSolar().toYmd());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user