Skip to content

Commit

Permalink
增加建除十二神;修复由闰月引起的月干支计算错误的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Apr 8, 2020
1 parent bab9d93 commit 6a2b395
Showing 2 changed files with 52 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/main/java/com/nlf/calendar/Lunar.java
Original file line number Diff line number Diff line change
@@ -553,7 +553,11 @@ public String getMonthInGanZhi(){
* @return 月天干,如己
*/
public String getMonthGan(){
int m = Math.abs(month)-1;
int m = Math.abs(month);
int leapMonth = LunarUtil.getLeapMonth(year);
if(0==leapMonth||m<leapMonth||month==leapMonth){
m-=1;
}
int yearGanIndex = (year-4)%10;
int offset = (yearGanIndex%5+1)*2;
return LunarUtil.GAN[(m+offset)%10+1];
@@ -564,7 +568,11 @@ public String getMonthGan(){
* @return 月地支,如卯
*/
public String getMonthZhi(){
int m = Math.abs(month)-1;
int m = Math.abs(month);
int leapMonth = LunarUtil.getLeapMonth(year);
if(0==leapMonth||m<leapMonth||month==leapMonth){
m-=1;
}
return LunarUtil.ZHI[(m+LunarUtil.BASE_MONTH_ZHI_INDEX)%12+1];
}

@@ -871,6 +879,34 @@ public List<String> getBaZiShiShenZhi(){
return l;
}

/**
* 获取建除十二神,当月支与日支相同即为建,依次类推
* @return 十二神
*/
public String getShiErShen(){
String monthZhi = getMonthZhi();
String dayZhi = getDayZhi();
int indexMonthZhi = 0;
int indexDayZhi = 0;
for(int i=0,j=LunarUtil.ZHI.length;i<j;i++){
String zhi = LunarUtil.ZHI[i];
if(zhi.equals(monthZhi)){
indexMonthZhi = i;
}
if(zhi.equals(dayZhi)){
indexDayZhi = i;
}
if(indexMonthZhi>0&&indexDayZhi>0){
break;
}
}
int add = indexDayZhi-indexMonthZhi;
if(add<0){
add = 12+add;
}
return LunarUtil.SHI_ER_SHEN[1+add];
}

public String toFullString(){
StringBuilder s = new StringBuilder();
s.append(toString());
14 changes: 14 additions & 0 deletions src/main/java/com/nlf/calendar/util/LunarUtil.java
Original file line number Diff line number Diff line change
@@ -47,6 +47,8 @@ public class LunarUtil{
public static final String[] POSITION_CAI = {"","艮","艮","坤","坤","坎","坎","震","震","离","离"};
/** 地支 */
public static final String[] ZHI = {"","子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
/** 建除十二神 */
public static final String[] SHI_ER_SHEN = {"","建","除","满","平","定","执","破","危","成","收","开","闭"};
/** 彭祖百忌.天干 */
public static final String[] PENGZU_GAN = {"","甲不开仓财物耗散","乙不栽植千株不长","丙不修灶必见灾殃","丁不剃头头必生疮","戊不受田田主不祥","己不破券二比并亡","庚不经络织机虚张","辛不合酱主人不尝","壬不泱水更难提防","癸不词讼理弱敌强"};
/** 彭祖百忌.地支 */
@@ -1095,6 +1097,18 @@ public static int computeAddDays(int year,int month,int day){
return diff;
}

/**
* 获取指定年份的闰月
* @param year 年份
* @return 闰月数字,1代表闰1月,0代表无闰月
*/
public static int getLeapMonth(int year){
int index = year-BASE_YEAR+BASE_INDEX;
int v = LUNAR_MONTH[2*index+1];
v = (v>>4)&0x0F;
return v;
}

/**
* 获取指定年月的下一个月是第几月
* @param y 农历年

0 comments on commit 6a2b395

Please sign in to comment.