1
0
mirror of synced 2026-04-06 11:48:43 +08:00

Merge branch 'v1.1.0-SNAPSHOT'

This commit is contained in:
6tail
2020-12-17 23:03:49 +08:00
4 changed files with 157 additions and 28 deletions

View File

@@ -44,6 +44,11 @@ public class EightChar {
} }
}; };
/**
* 流派2晚子时日柱按当天1晚子时日柱按明天
*/
protected int sect = 2;
/** /**
* 阴历 * 阴历
*/ */
@@ -62,6 +67,24 @@ public class EightChar {
return getYear() + " " + getMonth() + " " + getDay() + " " + getTime(); return getYear() + " " + getMonth() + " " + getDay() + " " + getTime();
} }
/**
* 获取流派
*
* @return 流派2晚子时日柱按当天1晚子时日柱按明天
*/
public int getSect() {
return sect;
}
/**
* 设置流派
*
* @param sect 流派2晚子时日柱按当天1晚子时日柱按明天其他值默认为2
*/
public void setSect(int sect) {
this.sect = (1 == sect) ? 1 : 2;
}
/** /**
* 获取年柱 * 获取年柱
* *
@@ -104,7 +127,7 @@ public class EightChar {
* @return 五行 * @return 五行
*/ */
public String getYearWuXing() { public String getYearWuXing() {
return LunarUtil.WU_XING_GAN.get(lunar.getYearGanExact()) + LunarUtil.WU_XING_ZHI.get(lunar.getYearZhiExact()); return LunarUtil.WU_XING_GAN.get(getYearGan()) + LunarUtil.WU_XING_ZHI.get(getYearZhi());
} }
/** /**
@@ -143,9 +166,27 @@ public class EightChar {
return getShiShenZhi(getYearZhi()); return getShiShenZhi(getYearZhi());
} }
/**
* 获取日干下标
*
* @return 日干下标0-9
*/
public int getDayGanIndex() {
return 2 == sect ? lunar.getDayGanIndexExact2() : lunar.getDayGanIndexExact();
}
/**
* 获取日支下标
*
* @return 日支下标0-11
*/
public int getDayZhiIndex() {
return 2 == sect ? lunar.getDayZhiIndexExact2() : lunar.getDayZhiIndexExact();
}
private String getDiShi(int zhiIndex) { private String getDiShi(int zhiIndex) {
int offset = CHANG_SHENG_OFFSET.get(getDayGan()); int offset = CHANG_SHENG_OFFSET.get(getDayGan());
int index = offset + (lunar.getDayGanIndexExact() % 2 == 0 ? zhiIndex : -zhiIndex); int index = offset + (getDayGanIndex() % 2 == 0 ? zhiIndex : -zhiIndex);
if (index >= 12) { if (index >= 12) {
index -= 12; index -= 12;
} }
@@ -206,7 +247,7 @@ public class EightChar {
* @return 五行 * @return 五行
*/ */
public String getMonthWuXing() { public String getMonthWuXing() {
return LunarUtil.WU_XING_GAN.get(lunar.getMonthGanExact()) + LunarUtil.WU_XING_ZHI.get(lunar.getMonthZhiExact()); return LunarUtil.WU_XING_GAN.get(getMonthGan()) + LunarUtil.WU_XING_ZHI.get(getMonthZhi());
} }
/** /**
@@ -251,7 +292,7 @@ public class EightChar {
* @return 日柱 * @return 日柱
*/ */
public String getDay() { public String getDay() {
return lunar.getDayInGanZhiExact(); return 2 == sect ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact();
} }
/** /**
@@ -260,7 +301,7 @@ public class EightChar {
* @return 天干 * @return 天干
*/ */
public String getDayGan() { public String getDayGan() {
return lunar.getDayGanExact(); return 2 == sect ? lunar.getDayGanExact2() : lunar.getDayGanExact();
} }
/** /**
@@ -269,7 +310,7 @@ public class EightChar {
* @return 地支 * @return 地支
*/ */
public String getDayZhi() { public String getDayZhi() {
return lunar.getDayZhiExact(); return 2 == sect ? lunar.getDayZhiExact2() : lunar.getDayZhiExact();
} }
/** /**
@@ -287,7 +328,7 @@ public class EightChar {
* @return 五行 * @return 五行
*/ */
public String getDayWuXing() { public String getDayWuXing() {
return LunarUtil.WU_XING_GAN.get(lunar.getDayGanExact()) + LunarUtil.WU_XING_ZHI.get(lunar.getDayZhiExact()); return LunarUtil.WU_XING_GAN.get(getDayGan()) + LunarUtil.WU_XING_ZHI.get(getDayZhi());
} }
/** /**
@@ -323,7 +364,7 @@ public class EightChar {
* @return 地势 * @return 地势
*/ */
public String getDayDiShi() { public String getDayDiShi() {
return getDiShi(lunar.getDayZhiIndexExact()); return getDiShi(getDayZhiIndex());
} }
/** /**
@@ -526,65 +567,73 @@ public class EightChar {
/** /**
* 获取年柱所在旬 * 获取年柱所在旬
*
* @return 旬 * @return 旬
*/ */
public String getYearXun(){ public String getYearXun() {
return lunar.getYearXunExact(); return lunar.getYearXunExact();
} }
/** /**
* 获取年柱旬空(空亡) * 获取年柱旬空(空亡)
*
* @return 旬空(空亡) * @return 旬空(空亡)
*/ */
public String getYearXunKong(){ public String getYearXunKong() {
return lunar.getYearXunKongExact(); return lunar.getYearXunKongExact();
} }
/** /**
* 获取月柱所在旬 * 获取月柱所在旬
*
* @return 旬 * @return 旬
*/ */
public String getMonthXun(){ public String getMonthXun() {
return lunar.getMonthXunExact(); return lunar.getMonthXunExact();
} }
/** /**
* 获取月柱旬空(空亡) * 获取月柱旬空(空亡)
*
* @return 旬空(空亡) * @return 旬空(空亡)
*/ */
public String getMonthXunKong(){ public String getMonthXunKong() {
return lunar.getMonthXunKongExact(); return lunar.getMonthXunKongExact();
} }
/** /**
* 获取日柱所在旬 * 获取日柱所在旬
*
* @return 旬 * @return 旬
*/ */
public String getDayXun(){ public String getDayXun() {
return lunar.getDayXunExact(); return 2 == sect ? lunar.getDayXunExact2() : lunar.getDayXunExact();
} }
/** /**
* 获取日柱旬空(空亡) * 获取日柱旬空(空亡)
*
* @return 旬空(空亡) * @return 旬空(空亡)
*/ */
public String getDayXunKong(){ public String getDayXunKong() {
return lunar.getDayXunKongExact(); return 2 == sect ? lunar.getDayXunKongExact2() : lunar.getDayXunKongExact();
} }
/** /**
* 获取时柱所在旬 * 获取时柱所在旬
*
* @return 旬 * @return 旬
*/ */
public String getTimeXun(){ public String getTimeXun() {
return lunar.getTimeXun(); return lunar.getTimeXun();
} }
/** /**
* 获取时柱旬空(空亡) * 获取时柱旬空(空亡)
*
* @return 旬空(空亡) * @return 旬空(空亡)
*/ */
public String getTimeXunKong(){ public String getTimeXunKong() {
return lunar.getTimeXunKong(); return lunar.getTimeXunKong();
} }
} }

View File

@@ -55,10 +55,14 @@ public class Lunar{
private int dayGanIndex; private int dayGanIndex;
/** 日对应的地支下标0-11 */ /** 日对应的地支下标0-11 */
private int dayZhiIndex; private int dayZhiIndex;
/** 日对应的天干下标(最精确的,供八字用,晚子时算第二0-9 */ /** 日对应的天干下标(八字流派1晚子时日柱算明0-9 */
private int dayGanIndexExact; private int dayGanIndexExact;
/** 日对应的地支下标(最精确的,供八字用,晚子时算第二0-11 */ /** 日对应的地支下标(八字流派1晚子时日柱算明0-11 */
private int dayZhiIndexExact; private int dayZhiIndexExact;
/** 日对应的天干下标八字流派2晚子时日柱算当天0-9 */
private int dayGanIndexExact2;
/** 日对应的地支下标八字流派2晚子时日柱算当天0-11 */
private int dayZhiIndexExact2;
/** 月对应的天干下标以节交接当天起算0-9 */ /** 月对应的天干下标以节交接当天起算0-9 */
private int monthGanIndex; private int monthGanIndex;
/** 月对应的地支下标以节交接当天起算0-11 */ /** 月对应的地支下标以节交接当天起算0-11 */
@@ -564,7 +568,11 @@ public class Lunar{
int dayGanExact = dayGanIndex; int dayGanExact = dayGanIndex;
int dayZhiExact = dayZhiIndex; int dayZhiExact = dayZhiIndex;
// 晚子时(夜子/子夜)应算作第二 // 八字流派2晚子时(夜子/子夜)日柱算当
dayGanIndexExact2 = dayGanExact;
dayZhiIndexExact2 = dayZhiExact;
// 八字流派1晚子时夜子/子夜)日柱算明天
String hm = (hour<10?"0":"")+hour+":"+(minute<10?"0":"")+minute; String hm = (hour<10?"0":"")+hour+":"+(minute<10?"0":"")+minute;
if(hm.compareTo("23:00")>=0&&hm.compareTo("23:59")<=0){ if(hm.compareTo("23:00")>=0&&hm.compareTo("23:59")<=0){
dayGanExact++; dayGanExact++;
@@ -807,7 +815,7 @@ public class Lunar{
} }
/** /**
* 获取干支纪日(日柱,晚子时算第二天) * 获取干支纪日(日柱,晚子时日柱算明天)
* *
* @return 干支纪日(日柱),如己卯 * @return 干支纪日(日柱),如己卯
*/ */
@@ -815,6 +823,15 @@ public class Lunar{
return getDayGanExact()+getDayZhiExact(); return getDayGanExact()+getDayZhiExact();
} }
/**
* 获取干支纪日(日柱,晚子时日柱算当天)
*
* @return 干支纪日(日柱),如己卯
*/
public String getDayInGanZhiExact2(){
return getDayGanExact2()+getDayZhiExact2();
}
/** /**
* 获取日天干 * 获取日天干
* *
@@ -825,7 +842,7 @@ public class Lunar{
} }
/** /**
* 获取日天干(晚子时算第二天) * 获取日天干(晚子时日柱算明天)
* *
* @return 日天干,如甲 * @return 日天干,如甲
*/ */
@@ -833,6 +850,15 @@ public class Lunar{
return LunarUtil.GAN[dayGanIndexExact+1]; return LunarUtil.GAN[dayGanIndexExact+1];
} }
/**
* 获取日天干(晚子时日柱算当天)
*
* @return 日天干,如甲
*/
public String getDayGanExact2(){
return LunarUtil.GAN[dayGanIndexExact2+1];
}
/** /**
* 获取日地支 * 获取日地支
* *
@@ -843,7 +869,7 @@ public class Lunar{
} }
/** /**
* 获取日地支(晚子时算第二天) * 获取日地支(晚子时日柱算明天)
* *
* @return 日地支,如卯 * @return 日地支,如卯
*/ */
@@ -851,6 +877,15 @@ public class Lunar{
return LunarUtil.ZHI[dayZhiIndexExact+1]; return LunarUtil.ZHI[dayZhiIndexExact+1];
} }
/**
* 获取日地支(晚子时日柱算当天)
*
* @return 日地支,如卯
*/
public String getDayZhiExact2(){
return LunarUtil.ZHI[dayZhiIndexExact2+1];
}
/** /**
* 获取年生肖 * 获取年生肖
* *
@@ -2308,10 +2343,18 @@ public class Lunar{
return dayGanIndexExact; return dayGanIndexExact;
} }
public int getDayGanIndexExact2() {
return dayGanIndexExact2;
}
public int getDayZhiIndexExact() { public int getDayZhiIndexExact() {
return dayZhiIndexExact; return dayZhiIndexExact;
} }
public int getDayZhiIndexExact2() {
return dayZhiIndexExact2;
}
public int getMonthGanIndexExact() { public int getMonthGanIndexExact() {
return monthGanIndexExact; return monthGanIndexExact;
} }
@@ -2479,13 +2522,21 @@ public class Lunar{
} }
/** /**
* 获取日所在旬(晚子时算第二天) * 获取日所在旬(晚子时日柱算明天)
* @return 旬 * @return 旬
*/ */
public String getDayXunExact(){ public String getDayXunExact(){
return LunarUtil.getXun(getDayInGanZhiExact()); return LunarUtil.getXun(getDayInGanZhiExact());
} }
/**
* 获取日所在旬(晚子时日柱算当天)
* @return 旬
*/
public String getDayXunExact2(){
return LunarUtil.getXun(getDayInGanZhiExact2());
}
/** /**
* 获取值日空亡 * 获取值日空亡
* @return 空亡(旬空) * @return 空亡(旬空)
@@ -2495,13 +2546,21 @@ public class Lunar{
} }
/** /**
* 获取值日空亡(晚子时算第二天) * 获取值日空亡(晚子时日柱算明天)
* @return 空亡(旬空) * @return 空亡(旬空)
*/ */
public String getDayXunKongExact(){ public String getDayXunKongExact(){
return LunarUtil.getXunKong(getDayInGanZhiExact()); return LunarUtil.getXunKong(getDayInGanZhiExact());
} }
/**
* 获取值日空亡(晚子时日柱算当天)
* @return 空亡(旬空)
*/
public String getDayXunKongExact2(){
return LunarUtil.getXunKong(getDayInGanZhiExact2());
}
/** /**
* 获取时辰所在旬 * 获取时辰所在旬
* @return 旬 * @return 旬

View File

@@ -208,7 +208,7 @@ public class Solar{
} }
/** /**
* 通过八字获取阳历列表 * 通过八字获取阳历列表(晚子时日柱按当天)
* @param yearGanZhi 年柱 * @param yearGanZhi 年柱
* @param monthGanZhi 月柱 * @param monthGanZhi 月柱
* @param dayGanZhi 日柱 * @param dayGanZhi 日柱
@@ -216,6 +216,20 @@ public class Solar{
* @return 符合的阳历列表 * @return 符合的阳历列表
*/ */
public static List<Solar> fromBaZi(String yearGanZhi,String monthGanZhi,String dayGanZhi,String timeGanZhi){ public static List<Solar> fromBaZi(String yearGanZhi,String monthGanZhi,String dayGanZhi,String timeGanZhi){
return fromBaZi(yearGanZhi,monthGanZhi,dayGanZhi,timeGanZhi,2);
}
/**
* 通过八字获取阳历列表
* @param yearGanZhi 年柱
* @param monthGanZhi 月柱
* @param dayGanZhi 日柱
* @param timeGanZhi 时柱
* @param sect 流派2晚子时日柱按当天1晚子时日柱按明天
* @return 符合的阳历列表
*/
public static List<Solar> fromBaZi(String yearGanZhi,String monthGanZhi,String dayGanZhi,String timeGanZhi,int sect){
sect = (1==sect)?1:2;
List<Solar> l = new ArrayList<Solar>(); List<Solar> l = new ArrayList<Solar>();
Solar today = new Solar(); Solar today = new Solar();
Lunar lunar = today.getLunar(); Lunar lunar = today.getLunar();
@@ -271,7 +285,8 @@ public class Solar{
Solar solar = new Solar(year, month, day, hour, 0, 0); Solar solar = new Solar(year, month, day, hour, 0, 0);
while (counter < 61) { while (counter < 61) {
lunar = solar.getLunar(); lunar = solar.getLunar();
if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi) && lunar.getDayInGanZhiExact().equals(dayGanZhi) && lunar.getTimeInGanZhi().equals(timeGanZhi)) { String dgz = (2==sect)?lunar.getDayInGanZhiExact2():lunar.getDayInGanZhiExact();
if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi) && dgz.equals(dayGanZhi) && lunar.getTimeInGanZhi().equals(timeGanZhi)) {
l.add(solar); l.add(solar);
break; break;
} }

View File

@@ -29,6 +29,12 @@ public class BaZiTest {
EightChar eightChar = lunar.getEightChar(); EightChar eightChar = lunar.getEightChar();
Assert.assertEquals("年柱", "戊辰", eightChar.getYear()); Assert.assertEquals("年柱", "戊辰", eightChar.getYear());
Assert.assertEquals("月柱", "甲寅", eightChar.getMonth()); Assert.assertEquals("月柱", "甲寅", eightChar.getMonth());
Assert.assertEquals("日柱", "庚子", eightChar.getDay());
Assert.assertEquals("时柱", "戊子", eightChar.getTime());
eightChar.setSect(1);
Assert.assertEquals("年柱", "戊辰", eightChar.getYear());
Assert.assertEquals("月柱", "甲寅", eightChar.getMonth());
Assert.assertEquals("日柱", "辛丑", eightChar.getDay()); Assert.assertEquals("日柱", "辛丑", eightChar.getDay());
Assert.assertEquals("时柱", "戊子", eightChar.getTime()); Assert.assertEquals("时柱", "戊子", eightChar.getTime());