Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
392ac5b00a | ||
|
|
9ec735cd4b | ||
|
|
e06c7a0532 | ||
|
|
bd18254834 | ||
|
|
28dbf8f45f | ||
|
|
0d1f64664e | ||
|
|
cc2bbb5049 | ||
|
|
9bb808da35 | ||
|
|
e29ab49618 | ||
|
|
60634d7cd8 | ||
|
|
fba9a85135 | ||
|
|
913e7b40fb | ||
|
|
c7738352eb | ||
|
|
64fc022ab4 | ||
|
|
6028aefbf2 | ||
|
|
78cad19748 |
@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.6tail</groupId>
|
<groupId>cn.6tail</groupId>
|
||||||
<artifactId>lunar</artifactId>
|
<artifactId>lunar</artifactId>
|
||||||
<version>1.1.2</version>
|
<version>1.1.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
|
|||||||
|
|
||||||
### 下载jar
|
### 下载jar
|
||||||
|
|
||||||
如果使用jar,建议下载最新的SNAPSHOT版本,bug将得到及时的修复。
|
如果使用jar,建议下载最新的版本,bug将得到及时的修复。
|
||||||
|
|
||||||
[Download](https://github.com/6tail/lunar-java/releases)
|
[Download](https://github.com/6tail/lunar-java/releases)
|
||||||
|
|
||||||
|
|||||||
@@ -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.1.2</version>
|
<version>1.1.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ lunar is a calendar library for Solar and Chinese Lunar.
|
|||||||
|
|
||||||
### Download
|
### Download
|
||||||
|
|
||||||
If you will use jars in your projects, I suggest you to download latest snapshot versions with less bugs.
|
If you will use jars in your projects, I suggest you to download latest versions with less bugs.
|
||||||
|
|
||||||
[Download](https://github.com/6tail/lunar-java/releases)
|
[Download](https://github.com/6tail/lunar-java/releases)
|
||||||
|
|
||||||
|
|||||||
4
pom.xml
4
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.1.2</version>
|
<version>1.1.7</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>
|
||||||
@@ -60,7 +60,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>4.12</version>
|
<version>4.13.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.nlf.calendar;
|
package com.nlf.calendar;
|
||||||
|
|
||||||
|
import com.nlf.calendar.util.LunarUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 节气
|
* 节气
|
||||||
*
|
*
|
||||||
@@ -13,11 +15,22 @@ public class JieQi {
|
|||||||
/** 阳历日期 */
|
/** 阳历日期 */
|
||||||
private Solar solar;
|
private Solar solar;
|
||||||
|
|
||||||
|
/** 是否节令 */
|
||||||
|
private boolean jie;
|
||||||
|
|
||||||
|
/** 是否气令 */
|
||||||
|
private boolean qi;
|
||||||
|
|
||||||
public JieQi() {
|
public JieQi() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化
|
||||||
|
* @param name 名称
|
||||||
|
* @param solar 阳历日期
|
||||||
|
*/
|
||||||
public JieQi(String name, Solar solar) {
|
public JieQi(String name, Solar solar) {
|
||||||
this.name = name;
|
setName(name);
|
||||||
this.solar = solar;
|
this.solar = solar;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,6 +48,18 @@ public class JieQi {
|
|||||||
*/
|
*/
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
for(String key: LunarUtil.JIE){
|
||||||
|
if(key.equals(name)){
|
||||||
|
this.jie = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(String key: LunarUtil.QI){
|
||||||
|
if(key.equals(name)){
|
||||||
|
this.qi = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -53,4 +78,24 @@ public class JieQi {
|
|||||||
this.solar = solar;
|
this.solar = solar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否节令
|
||||||
|
* @return true/false
|
||||||
|
*/
|
||||||
|
public boolean isJie(){
|
||||||
|
return jie;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否气令
|
||||||
|
* @return true/false
|
||||||
|
*/
|
||||||
|
public boolean isQi() {
|
||||||
|
return qi;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString(){
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,22 @@ import com.nlf.calendar.util.SolarUtil;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
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 double SECOND_PER_RAD = 180 * 3600 / Math.PI;
|
private static final double SECOND_PER_RAD = 180 * 3600 / Math.PI;
|
||||||
/** 节气表,国标以冬至为首个节气 */
|
/** 节气表,国标以冬至为首个节气 */
|
||||||
@@ -389,20 +405,34 @@ public class Lunar{
|
|||||||
if (calcJieQi(w) > jd){
|
if (calcJieQi(w) > jd){
|
||||||
w -= 365.2422;
|
w -= 365.2422;
|
||||||
}
|
}
|
||||||
|
//追加上一农历年末的大雪
|
||||||
|
double q = calcJieQi(w - 15.2184);
|
||||||
|
jieQi.put(JIE_QI_PREPEND, Solar.fromJulianDay(qiAccurate2(q) + Solar.J2000));
|
||||||
int size = JIE_QI.length;
|
int size = JIE_QI.length;
|
||||||
for (int i=0;i<size;i++) {
|
for (int i=0;i<size;i++) {
|
||||||
double q = calcJieQi(w + 15.2184 * i);
|
q = calcJieQi(w + 15.2184 * i);
|
||||||
jieQi.put(JIE_QI[i], Solar.fromJulianDay(qiAccurate2(q) + Solar.J2000));
|
jieQi.put(JIE_QI[i], Solar.fromJulianDay(qiAccurate2(q) + Solar.J2000));
|
||||||
}
|
}
|
||||||
//追加下一农历年的冬至
|
//追加下一农历年初的冬至
|
||||||
double q = calcJieQi(w + 15.2184 * size);
|
q = calcJieQi(w + 15.2184 * size);
|
||||||
jieQi.put("DONG_ZHI", Solar.fromJulianDay(qiAccurate2(q) + Solar.J2000));
|
jieQi.put(JIE_QI_APPEND, Solar.fromJulianDay(qiAccurate2(q) + Solar.J2000));
|
||||||
|
|
||||||
|
//追加下一阳历年初的小寒
|
||||||
|
size++;
|
||||||
|
q = calcJieQi(w + 15.2184 * size);
|
||||||
|
jieQi.put(JIE_APPEND_SOLAR_FIRST, Solar.fromJulianDay(qiAccurate2(q) + Solar.J2000));
|
||||||
|
|
||||||
|
//追加下一阳历年初的大寒
|
||||||
|
size++;
|
||||||
|
q = calcJieQi(w + 15.2184 * size);
|
||||||
|
jieQi.put(QI_APPEND_SOLAR_SECOND, Solar.fromJulianDay(qiAccurate2(q) + Solar.J2000));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算干支纪年
|
* 计算干支纪年
|
||||||
*/
|
*/
|
||||||
private void computeYear(){
|
private void computeYear(){
|
||||||
|
//以正月初一开始
|
||||||
yearGanIndex = (year+LunarUtil.BASE_YEAR_GANZHI_INDEX)%10;
|
yearGanIndex = (year+LunarUtil.BASE_YEAR_GANZHI_INDEX)%10;
|
||||||
yearZhiIndex = (year+LunarUtil.BASE_YEAR_GANZHI_INDEX)%12;
|
yearZhiIndex = (year+LunarUtil.BASE_YEAR_GANZHI_INDEX)%12;
|
||||||
|
|
||||||
@@ -414,32 +444,57 @@ public class Lunar{
|
|||||||
int gExact = yearGanIndex;
|
int gExact = yearGanIndex;
|
||||||
int zExact = yearZhiIndex;
|
int zExact = yearZhiIndex;
|
||||||
|
|
||||||
|
//获取立春的阳历时刻
|
||||||
|
Solar liChun = jieQi.get("立春");
|
||||||
|
|
||||||
|
//阳历和阴历年份相同代表正月初一及以后
|
||||||
if(year==solar.getYear()){
|
if(year==solar.getYear()){
|
||||||
//获取立春的阳历时刻
|
|
||||||
Solar liChun = jieQi.get("立春");
|
|
||||||
//立春日期判断
|
//立春日期判断
|
||||||
if(solar.toYmd().compareTo(liChun.toYmd())<0) {
|
if(solar.toYmd().compareTo(liChun.toYmd())<0) {
|
||||||
g--;
|
g--;
|
||||||
if(g<0){
|
|
||||||
g += 10;
|
|
||||||
}
|
|
||||||
z--;
|
z--;
|
||||||
if(z<0){
|
|
||||||
z += 12;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//立春交接时刻判断
|
//立春交接时刻判断
|
||||||
if(solar.toYmdHms().compareTo(liChun.toYmdHms())<0) {
|
if(solar.toYmdHms().compareTo(liChun.toYmdHms())<0) {
|
||||||
gExact--;
|
gExact--;
|
||||||
if(gExact<0){
|
|
||||||
gExact += 10;
|
|
||||||
}
|
|
||||||
zExact--;
|
zExact--;
|
||||||
if(zExact<0){
|
}
|
||||||
zExact += 12;
|
}else{
|
||||||
}
|
if(solar.toYmd().compareTo(liChun.toYmd())>=0) {
|
||||||
|
g++;
|
||||||
|
z++;
|
||||||
|
}
|
||||||
|
if(solar.toYmdHms().compareTo(liChun.toYmdHms())>=0) {
|
||||||
|
gExact++;
|
||||||
|
zExact++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(g<0){
|
||||||
|
g += 10;
|
||||||
|
}
|
||||||
|
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;
|
yearGanIndexByLiChun = g;
|
||||||
yearZhiIndexByLiChun = z;
|
yearZhiIndexByLiChun = z;
|
||||||
|
|
||||||
@@ -926,9 +981,9 @@ public class Lunar{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取节
|
* 获取节令
|
||||||
*
|
*
|
||||||
* @return 节
|
* @return 节令
|
||||||
*/
|
*/
|
||||||
public String getJie(){
|
public String getJie(){
|
||||||
for(String jie:LunarUtil.JIE){
|
for(String jie:LunarUtil.JIE){
|
||||||
@@ -937,13 +992,23 @@ public class Lunar{
|
|||||||
return jie;
|
return 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 "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取气
|
* 获取气令
|
||||||
*
|
*
|
||||||
* @return 气
|
* @return 气令
|
||||||
*/
|
*/
|
||||||
public String getQi(){
|
public String getQi(){
|
||||||
for(String qi:LunarUtil.QI){
|
for(String qi:LunarUtil.QI){
|
||||||
@@ -952,9 +1017,15 @@ public class Lunar{
|
|||||||
return qi;
|
return qi;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Solar d = jieQi.get("DONG_ZHI");
|
// 追加的气令:冬至
|
||||||
|
Solar d = jieQi.get(JIE_QI_APPEND);
|
||||||
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 "冬至";
|
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 "";
|
return "";
|
||||||
}
|
}
|
||||||
@@ -1832,8 +1903,11 @@ public class Lunar{
|
|||||||
}
|
}
|
||||||
// 寅月起,所以需要-2
|
// 寅月起,所以需要-2
|
||||||
int monthIndex = monthZhiIndex-2;
|
int monthIndex = monthZhiIndex-2;
|
||||||
|
if(monthIndex<0){
|
||||||
|
monthIndex += 12;
|
||||||
|
}
|
||||||
int index = start-monthIndex-1;
|
int index = start-monthIndex-1;
|
||||||
if(index<0){
|
while(index<0){
|
||||||
index += 9;
|
index += 9;
|
||||||
}
|
}
|
||||||
return new NineStar(index);
|
return new NineStar(index);
|
||||||
@@ -1846,6 +1920,7 @@ public class Lunar{
|
|||||||
public NineStar getDayNineStar(){
|
public NineStar getDayNineStar(){
|
||||||
//顺逆
|
//顺逆
|
||||||
String solarYmd = solar.toYmd();
|
String solarYmd = solar.toYmd();
|
||||||
|
String dongZhi = jieQi.get("冬至").toYmd();
|
||||||
String yuShui = jieQi.get("雨水").toYmd();
|
String yuShui = jieQi.get("雨水").toYmd();
|
||||||
String guYu = jieQi.get("谷雨").toYmd();
|
String guYu = jieQi.get("谷雨").toYmd();
|
||||||
String xiaZhi = jieQi.get("夏至").toYmd();
|
String xiaZhi = jieQi.get("夏至").toYmd();
|
||||||
@@ -1854,7 +1929,7 @@ public class Lunar{
|
|||||||
|
|
||||||
int start = 6;
|
int start = 6;
|
||||||
boolean asc = false;
|
boolean asc = false;
|
||||||
if(solarYmd.compareTo(jieQi.get("冬至").toYmd())>=0 && solarYmd.compareTo(yuShui)<0){
|
if(solarYmd.compareTo(dongZhi)>=0 && solarYmd.compareTo(yuShui)<0){
|
||||||
asc = true;
|
asc = true;
|
||||||
start = 1;
|
start = 1;
|
||||||
} else if(solarYmd.compareTo(yuShui)>=0 && solarYmd.compareTo(guYu)<0){
|
} else if(solarYmd.compareTo(yuShui)>=0 && solarYmd.compareTo(guYu)<0){
|
||||||
@@ -1916,7 +1991,7 @@ public class Lunar{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取下一节(顺推的第一个节)
|
* 获取下一节令(顺推的第一个节令)
|
||||||
* @return 节气
|
* @return 节气
|
||||||
*/
|
*/
|
||||||
public JieQi getNextJie(){
|
public JieQi getNextJie(){
|
||||||
@@ -1924,13 +1999,29 @@ public class Lunar{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取上一节(逆推的第一个节)
|
* 获取上一节令(逆推的第一个节令)
|
||||||
* @return 节气
|
* @return 节气
|
||||||
*/
|
*/
|
||||||
public JieQi getPrevJie(){
|
public JieQi getPrevJie(){
|
||||||
return getNearJieQi(false,LunarUtil.JIE);
|
return getNearJieQi(false,LunarUtil.JIE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取下一气令(顺推的第一个气令)
|
||||||
|
* @return 节气
|
||||||
|
*/
|
||||||
|
public JieQi getNextQi(){
|
||||||
|
return getNearJieQi(true,LunarUtil.QI);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上一气令(逆推的第一个气令)
|
||||||
|
* @return 节气
|
||||||
|
*/
|
||||||
|
public JieQi getPrevQi(){
|
||||||
|
return getNearJieQi(false,LunarUtil.QI);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取下一节气(顺推的第一个节气)
|
* 获取下一节气(顺推的第一个节气)
|
||||||
* @return 节气
|
* @return 节气
|
||||||
@@ -1964,8 +2055,17 @@ public class Lunar{
|
|||||||
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 = entry.getKey();
|
||||||
if("DONG_ZHI".equals(jq)){
|
if(JIE_QI_APPEND.equals(jq)){
|
||||||
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)){
|
||||||
@@ -1998,6 +2098,58 @@ public class Lunar{
|
|||||||
return new JieQi(name, near);
|
return new JieQi(name, near);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取节气名称,如果无节气,返回空字符串
|
||||||
|
* @return 节气名称
|
||||||
|
*/
|
||||||
|
public String getJieQi(){
|
||||||
|
String name = "";
|
||||||
|
for(Map.Entry<String,Solar> jq:jieQi.entrySet()){
|
||||||
|
Solar d = jq.getValue();
|
||||||
|
if(d.getYear()==solar.getYear()&&d.getMonth()==solar.getMonth()&&d.getDay()==solar.getDay()){
|
||||||
|
name = jq.getKey();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(JIE_QI_APPEND.equals(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当天节气对象,如果无节气,返回null
|
||||||
|
* @return 节气对象
|
||||||
|
*/
|
||||||
|
public JieQi getCurrentJieQi(){
|
||||||
|
String name = getJieQi();
|
||||||
|
return name.length()>0 ? new JieQi(name,solar) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当天节令对象,如果无节令,返回null
|
||||||
|
* @return 节气对象
|
||||||
|
*/
|
||||||
|
public JieQi getCurrentJie(){
|
||||||
|
String name = getJie();
|
||||||
|
return name.length()>0 ? new JieQi(name,solar) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当天气令对象,如果无气令,返回null
|
||||||
|
* @return 节气对象
|
||||||
|
*/
|
||||||
|
public JieQi getCurrentQi(){
|
||||||
|
String name = getQi();
|
||||||
|
return name.length()>0 ? new JieQi(name,solar) : null;
|
||||||
|
}
|
||||||
|
|
||||||
public String toFullString(){
|
public String toFullString(){
|
||||||
StringBuilder s = new StringBuilder();
|
StringBuilder s = new StringBuilder();
|
||||||
s.append(toString());
|
s.append(toString());
|
||||||
@@ -2037,7 +2189,7 @@ public class Lunar{
|
|||||||
s.append(f);
|
s.append(f);
|
||||||
s.append(")");
|
s.append(")");
|
||||||
}
|
}
|
||||||
String jq = getJie()+getQi();
|
String jq = getJieQi();
|
||||||
if(jq.length()>0){
|
if(jq.length()>0){
|
||||||
s.append(" [");
|
s.append(" [");
|
||||||
s.append(jq);
|
s.append(jq);
|
||||||
@@ -2186,4 +2338,55 @@ public class Lunar{
|
|||||||
}
|
}
|
||||||
return eightChar;
|
return eightChar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取往后推几天的农历日期,如果要往前推,则天数用负数
|
||||||
|
* @param days 天数
|
||||||
|
* @return 农历日期
|
||||||
|
*/
|
||||||
|
public Lunar next(int days){
|
||||||
|
int y = year;
|
||||||
|
int m = month;
|
||||||
|
int d = day;
|
||||||
|
if(days>0){
|
||||||
|
int daysInMonth = LunarUtil.getDaysOfMonth(y,m);
|
||||||
|
int rest = day+days;
|
||||||
|
while(daysInMonth < rest) {
|
||||||
|
if(m>0){
|
||||||
|
if(LunarUtil.getLeapMonth(y)!=m){
|
||||||
|
m++;
|
||||||
|
}else{
|
||||||
|
m = -m;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
m = 1-m;
|
||||||
|
}
|
||||||
|
if(13==m){
|
||||||
|
y++;
|
||||||
|
m=1;
|
||||||
|
}
|
||||||
|
rest -= daysInMonth;
|
||||||
|
daysInMonth = LunarUtil.getDaysOfMonth(y,m);
|
||||||
|
}
|
||||||
|
d = rest;
|
||||||
|
}else if(days<0){
|
||||||
|
int daysInMonth = day;
|
||||||
|
int rest = -days;
|
||||||
|
while(daysInMonth <= rest) {
|
||||||
|
if(m>0){
|
||||||
|
m--;
|
||||||
|
if(0==m){
|
||||||
|
y--;
|
||||||
|
m = LunarUtil.getLeapMonth(y)!=12?12:-12;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
m = -m;
|
||||||
|
}
|
||||||
|
rest -= daysInMonth;
|
||||||
|
daysInMonth = LunarUtil.getDaysOfMonth(y,m);
|
||||||
|
}
|
||||||
|
d = daysInMonth - rest;
|
||||||
|
}
|
||||||
|
return new Lunar(y,m,d,hour,minute,second);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1142,17 +1142,11 @@ public class LunarUtil{
|
|||||||
public static int nextMonth(int y,int m){
|
public static int nextMonth(int y,int m){
|
||||||
int n = Math.abs(m)+1;
|
int n = Math.abs(m)+1;
|
||||||
if(m>0){
|
if(m>0){
|
||||||
int index = y-BASE_YEAR+BASE_INDEX;
|
if(m==getLeapMonth(y)){
|
||||||
int v = LUNAR_MONTH[2*index+1];
|
|
||||||
v = (v>>4)&0x0F;
|
|
||||||
if(v==m){
|
|
||||||
n = -m;
|
n = -m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(n==13){
|
return 13!=n?n:1;
|
||||||
n = 1;
|
|
||||||
}
|
|
||||||
return n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -22,6 +22,33 @@ public class BaZiTest {
|
|||||||
Assert.assertEquals("时柱", "壬辰", eightChar.getTime());
|
Assert.assertEquals("时柱", "壬辰", eightChar.getTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGanZhi2() {
|
||||||
|
Solar solar = new Solar(1988, 2, 15, 23, 30, 0);
|
||||||
|
Lunar lunar = solar.getLunar();
|
||||||
|
EightChar eightChar = lunar.getEightChar();
|
||||||
|
Assert.assertEquals("年柱", "戊辰", eightChar.getYear());
|
||||||
|
Assert.assertEquals("月柱", "甲寅", eightChar.getMonth());
|
||||||
|
Assert.assertEquals("日柱", "辛丑", eightChar.getDay());
|
||||||
|
Assert.assertEquals("时柱", "戊子", eightChar.getTime());
|
||||||
|
|
||||||
|
solar = new Solar(1988, 2, 15, 22, 30, 0);
|
||||||
|
lunar = solar.getLunar();
|
||||||
|
eightChar = lunar.getEightChar();
|
||||||
|
Assert.assertEquals("年柱", "戊辰", eightChar.getYear());
|
||||||
|
Assert.assertEquals("月柱", "甲寅", eightChar.getMonth());
|
||||||
|
Assert.assertEquals("日柱", "庚子", eightChar.getDay());
|
||||||
|
Assert.assertEquals("时柱", "丁亥", eightChar.getTime());
|
||||||
|
|
||||||
|
solar = new Solar(1988, 2, 2, 22, 30, 0);
|
||||||
|
lunar = solar.getLunar();
|
||||||
|
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);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package test;
|
package test;
|
||||||
|
|
||||||
|
import com.nlf.calendar.Lunar;
|
||||||
import com.nlf.calendar.Solar;
|
import com.nlf.calendar.Solar;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -95,4 +96,81 @@ public class JieQiTest {
|
|||||||
Assert.assertEquals(name, entry.getValue(), result.get(name).toYmdHms());
|
Assert.assertEquals(name, entry.getValue(), result.get(name).toYmdHms());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
Solar solar = Solar.fromYmd(1986,1,5);
|
||||||
|
Lunar lunar = solar.getLunar();
|
||||||
|
Assert.assertEquals("小寒", lunar.getJie());
|
||||||
|
Assert.assertEquals("小寒", lunar.getJieQi());
|
||||||
|
Assert.assertEquals("小寒", lunar.getCurrentJieQi().getName());
|
||||||
|
Assert.assertEquals("小寒", lunar.getCurrentJie().getName());
|
||||||
|
Assert.assertNull(lunar.getCurrentQi());
|
||||||
|
Assert.assertEquals("", lunar.getQi());
|
||||||
|
Assert.assertEquals("大雪", lunar.getPrevJie().getName());
|
||||||
|
Assert.assertEquals("冬至", lunar.getPrevQi().getName());
|
||||||
|
Assert.assertEquals("冬至", lunar.getPrevJieQi().getName());
|
||||||
|
|
||||||
|
solar = Solar.fromYmdHms(1986,1,20,17,0,0);
|
||||||
|
lunar = solar.getLunar();
|
||||||
|
Assert.assertEquals("大寒", lunar.getQi());
|
||||||
|
Assert.assertEquals("大寒", lunar.getJieQi());
|
||||||
|
Assert.assertEquals("大寒", lunar.getCurrentJieQi().getName());
|
||||||
|
Assert.assertEquals("大寒", lunar.getCurrentQi().getName());
|
||||||
|
Assert.assertNull(lunar.getCurrentJie());
|
||||||
|
Assert.assertEquals("", lunar.getJie());
|
||||||
|
Assert.assertEquals("立春", lunar.getNextJie().getName());
|
||||||
|
Assert.assertEquals("雨水", lunar.getNextQi().getName());
|
||||||
|
Assert.assertEquals("立春", lunar.getNextJieQi().getName());
|
||||||
|
solar = Solar.fromYmdHms(1986,1,20,14,0,0);
|
||||||
|
lunar = solar.getLunar();
|
||||||
|
Assert.assertEquals("小寒", lunar.getPrevJie().getName());
|
||||||
|
Assert.assertEquals("冬至", lunar.getPrevQi().getName());
|
||||||
|
Assert.assertEquals("小寒", lunar.getPrevJieQi().getName());
|
||||||
|
|
||||||
|
solar = Solar.fromYmd(1986,12,7);
|
||||||
|
lunar = solar.getLunar();
|
||||||
|
Assert.assertEquals("大雪", lunar.getJie());
|
||||||
|
Assert.assertEquals("大雪", lunar.getJieQi());
|
||||||
|
Assert.assertEquals("大雪", lunar.getCurrentJieQi().getName());
|
||||||
|
Assert.assertEquals("大雪", lunar.getCurrentJie().getName());
|
||||||
|
Assert.assertNull(lunar.getCurrentQi());
|
||||||
|
Assert.assertEquals("", lunar.getQi());
|
||||||
|
Assert.assertEquals("大雪", lunar.getNextJie().getName());
|
||||||
|
Assert.assertEquals("冬至", lunar.getNextQi().getName());
|
||||||
|
Assert.assertEquals("大雪", lunar.getNextJieQi().getName());
|
||||||
|
|
||||||
|
solar = Solar.fromYmd(1986,1,1);
|
||||||
|
lunar = solar.getLunar();
|
||||||
|
Assert.assertEquals("", lunar.getJie());
|
||||||
|
Assert.assertEquals("", lunar.getQi());
|
||||||
|
Assert.assertEquals("", lunar.getJieQi());
|
||||||
|
Assert.assertNull(lunar.getCurrentJieQi());
|
||||||
|
Assert.assertNull(lunar.getCurrentJie());
|
||||||
|
Assert.assertNull(lunar.getCurrentQi());
|
||||||
|
Assert.assertEquals("大雪", lunar.getPrevJie().getName());
|
||||||
|
Assert.assertEquals("冬至", lunar.getPrevQi().getName());
|
||||||
|
Assert.assertEquals("冬至", lunar.getPrevJieQi().getName());
|
||||||
|
Assert.assertEquals("小寒", lunar.getNextJie().getName());
|
||||||
|
Assert.assertEquals("大寒", lunar.getNextQi().getName());
|
||||||
|
Assert.assertEquals("小寒", lunar.getNextJieQi().getName());
|
||||||
|
|
||||||
|
|
||||||
|
solar = Solar.fromYmd(2012,12,25);
|
||||||
|
lunar = solar.getLunar();
|
||||||
|
Assert.assertEquals("", lunar.getJie());
|
||||||
|
Assert.assertEquals("", lunar.getQi());
|
||||||
|
Assert.assertEquals("", lunar.getJieQi());
|
||||||
|
Assert.assertNull(lunar.getCurrentJie());
|
||||||
|
Assert.assertNull(lunar.getCurrentQi());
|
||||||
|
Assert.assertNull(lunar.getCurrentJieQi());
|
||||||
|
|
||||||
|
Assert.assertEquals("小寒", lunar.getNextJie().getName());
|
||||||
|
Assert.assertEquals("大寒", lunar.getNextQi().getName());
|
||||||
|
Assert.assertEquals("小寒", lunar.getNextJieQi().getName());
|
||||||
|
|
||||||
|
Assert.assertEquals("大雪", lunar.getPrevJie().getName());
|
||||||
|
Assert.assertEquals("冬至", lunar.getPrevQi().getName());
|
||||||
|
Assert.assertEquals("冬至", lunar.getPrevJieQi().getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package test;
|
package test;
|
||||||
|
|
||||||
import com.nlf.calendar.Lunar;
|
import com.nlf.calendar.Lunar;
|
||||||
|
import com.nlf.calendar.Solar;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@@ -20,4 +21,13 @@ public class LunarTest {
|
|||||||
Assert.assertEquals("2019-05-01 00:00:00 星期三 (劳动节) 金牛座",date.getSolar().toFullString());
|
Assert.assertEquals("2019-05-01 00:00:00 星期三 (劳动节) 金牛座",date.getSolar().toFullString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNext(){
|
||||||
|
Solar solar = new Solar(2020,1,10,12,0,0);
|
||||||
|
Lunar lunar = solar.getLunar();
|
||||||
|
for(int i=-500;i<500;i++){
|
||||||
|
Assert.assertEquals("推移天数:"+i,solar.next(i).getLunar().toFullString(),lunar.next(i).toFullString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user