Skip to content

Commit

Permalink
Merge branch 'v1.1.0-SNAPSHOT'
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Oct 15, 2020
2 parents 60634d7 + e29ab49 commit 9bb808d
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
105 changes: 91 additions & 14 deletions src/main/java/com/nlf/calendar/Lunar.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ private void computeJieQi(){
* 计算干支纪年
*/
private void computeYear(){
//以正月初一开始
yearGanIndex = (year+LunarUtil.BASE_YEAR_GANZHI_INDEX)%10;
yearZhiIndex = (year+LunarUtil.BASE_YEAR_GANZHI_INDEX)%12;

Expand All @@ -425,32 +426,57 @@ private void computeYear(){
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;

Expand Down Expand Up @@ -2271,4 +2297,55 @@ public EightChar getEightChar(){
}
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);
}
}
10 changes: 2 additions & 8 deletions src/main/java/com/nlf/calendar/util/LunarUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1142,17 +1142,11 @@ public static int getLeapMonth(int year){
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;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/test/BaZiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ public void testGanZhi() {
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);
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/test/LunarTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test;

import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -20,4 +21,13 @@ public void test(){
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());
}
}

}

0 comments on commit 9bb808d

Please sign in to comment.