Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
392ac5b00a | ||
|
|
9ec735cd4b | ||
|
|
e06c7a0532 | ||
|
|
bd18254834 | ||
|
|
28dbf8f45f | ||
|
|
0d1f64664e | ||
|
|
cc2bbb5049 | ||
|
|
9bb808da35 | ||
|
|
e29ab49618 |
@@ -16,7 +16,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历)
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.6tail</groupId>
|
<groupId>cn.6tail</groupId>
|
||||||
<artifactId>lunar</artifactId>
|
<artifactId>lunar</artifactId>
|
||||||
<version>1.1.4</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.4</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.4</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>
|
||||||
|
|||||||
@@ -20,6 +20,14 @@ public class Lunar{
|
|||||||
public static final String JIE_QI_FIRST = "冬至";
|
public static final String JIE_QI_FIRST = "冬至";
|
||||||
/** 农历年末节气名(节令:大雪) */
|
/** 农历年末节气名(节令:大雪) */
|
||||||
public static final String JIE_QI_LAST = "大雪";
|
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;
|
||||||
/** 节气表,国标以冬至为首个节气 */
|
/** 节气表,国标以冬至为首个节气 */
|
||||||
@@ -408,12 +416,23 @@ public class Lunar{
|
|||||||
//追加下一农历年初的冬至
|
//追加下一农历年初的冬至
|
||||||
q = calcJieQi(w + 15.2184 * size);
|
q = calcJieQi(w + 15.2184 * size);
|
||||||
jieQi.put(JIE_QI_APPEND, 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;
|
||||||
|
|
||||||
@@ -425,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;
|
||||||
|
|
||||||
@@ -953,6 +997,11 @@ public class Lunar{
|
|||||||
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_QI_LAST;
|
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 "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -973,6 +1022,11 @@ public class Lunar{
|
|||||||
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_QI_FIRST;
|
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 "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1849,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);
|
||||||
@@ -2004,6 +2061,12 @@ public class Lunar{
|
|||||||
if(JIE_QI_PREPEND.equals(jq)){
|
if(JIE_QI_PREPEND.equals(jq)){
|
||||||
jq = JIE_QI_LAST;
|
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;
|
||||||
@@ -2052,6 +2115,10 @@ public class Lunar{
|
|||||||
name = JIE_QI_FIRST;
|
name = JIE_QI_FIRST;
|
||||||
}else if(JIE_QI_PREPEND.equals(name)){
|
}else if(JIE_QI_PREPEND.equals(name)){
|
||||||
name = JIE_QI_LAST;
|
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;
|
return name;
|
||||||
}
|
}
|
||||||
@@ -2271,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);
|
||||||
|
|||||||
@@ -154,5 +154,23 @@ public class JieQiTest {
|
|||||||
Assert.assertEquals("小寒", lunar.getNextJie().getName());
|
Assert.assertEquals("小寒", lunar.getNextJie().getName());
|
||||||
Assert.assertEquals("大寒", lunar.getNextQi().getName());
|
Assert.assertEquals("大寒", lunar.getNextQi().getName());
|
||||||
Assert.assertEquals("小寒", lunar.getNextJieQi().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