修复立春至正月初一之间八字年月柱错误的问题;新增支持农历日期的推移。
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -60,7 +60,7 @@
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -414,6 +414,7 @@ public class Lunar{
|
||||
* 计算干支纪年
|
||||
*/
|
||||
private void computeYear(){
|
||||
//以正月初一开始
|
||||
yearGanIndex = (year+LunarUtil.BASE_YEAR_GANZHI_INDEX)%10;
|
||||
yearZhiIndex = (year+LunarUtil.BASE_YEAR_GANZHI_INDEX)%12;
|
||||
|
||||
@@ -425,32 +426,57 @@ public class Lunar{
|
||||
int gExact = yearGanIndex;
|
||||
int zExact = yearZhiIndex;
|
||||
|
||||
//获取立春的阳历时刻
|
||||
Solar liChun = jieQi.get("立春");
|
||||
|
||||
//阳历和阴历年份相同代表正月初一及以后
|
||||
if(year==solar.getYear()){
|
||||
//获取立春的阳历时刻
|
||||
Solar liChun = jieQi.get("立春");
|
||||
//立春日期判断
|
||||
if(solar.toYmd().compareTo(liChun.toYmd())<0) {
|
||||
g--;
|
||||
if(g<0){
|
||||
g += 10;
|
||||
}
|
||||
z--;
|
||||
if(z<0){
|
||||
z += 12;
|
||||
}
|
||||
}
|
||||
//立春交接时刻判断
|
||||
if(solar.toYmdHms().compareTo(liChun.toYmdHms())<0) {
|
||||
gExact--;
|
||||
if(gExact<0){
|
||||
gExact += 10;
|
||||
}
|
||||
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;
|
||||
yearZhiIndexByLiChun = z;
|
||||
|
||||
@@ -2271,4 +2297,55 @@ public class Lunar{
|
||||
}
|
||||
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){
|
||||
int n = Math.abs(m)+1;
|
||||
if(m>0){
|
||||
int index = y-BASE_YEAR+BASE_INDEX;
|
||||
int v = LUNAR_MONTH[2*index+1];
|
||||
v = (v>>4)&0x0F;
|
||||
if(v==m){
|
||||
if(m==getLeapMonth(y)){
|
||||
n = -m;
|
||||
}
|
||||
}
|
||||
if(n==13){
|
||||
n = 1;
|
||||
}
|
||||
return n;
|
||||
return 13!=n?n:1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,33 @@ public class BaZiTest {
|
||||
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
|
||||
public void testHideGan() {
|
||||
Solar solar = new Solar(2005, 12, 23, 8, 37, 0);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package test;
|
||||
|
||||
import com.nlf.calendar.Lunar;
|
||||
import com.nlf.calendar.Solar;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -20,4 +21,13 @@ public class LunarTest {
|
||||
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