diff --git a/src/main/java/com/nlf/calendar/EightChar.java b/src/main/java/com/nlf/calendar/EightChar.java index 37b2c23..57bd057 100644 --- a/src/main/java/com/nlf/calendar/EightChar.java +++ b/src/main/java/com/nlf/calendar/EightChar.java @@ -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 流派,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 五行 */ 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 日干下标,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) { 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) { index -= 12; } @@ -206,7 +247,7 @@ public class EightChar { * @return 五行 */ 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 日柱 */ public String getDay() { - return lunar.getDayInGanZhiExact(); + return 2 == sect ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact(); } /** @@ -260,7 +301,7 @@ public class EightChar { * @return 天干 */ public String getDayGan() { - return lunar.getDayGanExact(); + return 2 == sect ? lunar.getDayGanExact2() : lunar.getDayGanExact(); } /** @@ -269,7 +310,7 @@ public class EightChar { * @return 地支 */ public String getDayZhi() { - return lunar.getDayZhiExact(); + return 2 == sect ? lunar.getDayZhiExact2() : lunar.getDayZhiExact(); } /** @@ -287,7 +328,7 @@ public class EightChar { * @return 五行 */ 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 地势 */ public String getDayDiShi() { - return getDiShi(lunar.getDayZhiIndexExact()); + return getDiShi(getDayZhiIndex()); } /** @@ -526,65 +567,73 @@ public class EightChar { /** * 获取年柱所在旬 + * * @return 旬 */ - public String getYearXun(){ + public String getYearXun() { return lunar.getYearXunExact(); } /** * 获取年柱旬空(空亡) + * * @return 旬空(空亡) */ - public String getYearXunKong(){ + public String getYearXunKong() { return lunar.getYearXunKongExact(); } /** * 获取月柱所在旬 + * * @return 旬 */ - public String getMonthXun(){ + public String getMonthXun() { return lunar.getMonthXunExact(); } /** * 获取月柱旬空(空亡) + * * @return 旬空(空亡) */ - public String getMonthXunKong(){ + public String getMonthXunKong() { return lunar.getMonthXunKongExact(); } /** * 获取日柱所在旬 + * * @return 旬 */ - public String getDayXun(){ - return lunar.getDayXunExact(); + public String getDayXun() { + return 2 == sect ? lunar.getDayXunExact2() : lunar.getDayXunExact(); } /** * 获取日柱旬空(空亡) + * * @return 旬空(空亡) */ - public String getDayXunKong(){ - return lunar.getDayXunKongExact(); + public String getDayXunKong() { + return 2 == sect ? lunar.getDayXunKongExact2() : lunar.getDayXunKongExact(); } /** * 获取时柱所在旬 + * * @return 旬 */ - public String getTimeXun(){ + public String getTimeXun() { return lunar.getTimeXun(); } /** * 获取时柱旬空(空亡) + * * @return 旬空(空亡) */ - public String getTimeXunKong(){ + public String getTimeXunKong() { return lunar.getTimeXunKong(); } } diff --git a/src/main/java/com/nlf/calendar/Lunar.java b/src/main/java/com/nlf/calendar/Lunar.java index 732fd78..9a4cc86 100644 --- a/src/main/java/com/nlf/calendar/Lunar.java +++ b/src/main/java/com/nlf/calendar/Lunar.java @@ -55,10 +55,14 @@ public class Lunar{ private int dayGanIndex; /** 日对应的地支下标,0-11 */ private int dayZhiIndex; - /** 日对应的天干下标(最精确的,供八字用,晚子时算第二天),0-9 */ + /** 日对应的天干下标(八字流派1,晚子时日柱算明天),0-9 */ private int dayGanIndexExact; - /** 日对应的地支下标(最精确的,供八字用,晚子时算第二天),0-11 */ + /** 日对应的地支下标(八字流派1,晚子时日柱算明天),0-11 */ private int dayZhiIndexExact; + /** 日对应的天干下标(八字流派2,晚子时日柱算当天),0-9 */ + private int dayGanIndexExact2; + /** 日对应的地支下标(八字流派2,晚子时日柱算当天),0-11 */ + private int dayZhiIndexExact2; /** 月对应的天干下标(以节交接当天起算),0-9 */ private int monthGanIndex; /** 月对应的地支下标(以节交接当天起算),0-11 */ @@ -564,7 +568,11 @@ public class Lunar{ int dayGanExact = dayGanIndex; int dayZhiExact = dayZhiIndex; - // 晚子时(夜子/子夜)应算作第二天 + // 八字流派2,晚子时(夜子/子夜)日柱算当天 + dayGanIndexExact2 = dayGanExact; + dayZhiIndexExact2 = dayZhiExact; + + // 八字流派1,晚子时(夜子/子夜)日柱算明天 String hm = (hour<10?"0":"")+hour+":"+(minute<10?"0":"")+minute; if(hm.compareTo("23:00")>=0&&hm.compareTo("23:59")<=0){ dayGanExact++; @@ -807,7 +815,7 @@ public class Lunar{ } /** - * 获取干支纪日(日柱,晚子时算第二天) + * 获取干支纪日(日柱,晚子时日柱算明天) * * @return 干支纪日(日柱),如己卯 */ @@ -815,6 +823,15 @@ public class Lunar{ return getDayGanExact()+getDayZhiExact(); } + /** + * 获取干支纪日(日柱,晚子时日柱算当天) + * + * @return 干支纪日(日柱),如己卯 + */ + public String getDayInGanZhiExact2(){ + return getDayGanExact2()+getDayZhiExact2(); + } + /** * 获取日天干 * @@ -825,7 +842,7 @@ public class Lunar{ } /** - * 获取日天干(晚子时算第二天) + * 获取日天干(晚子时日柱算明天) * * @return 日天干,如甲 */ @@ -833,6 +850,15 @@ public class Lunar{ return LunarUtil.GAN[dayGanIndexExact+1]; } + /** + * 获取日天干(晚子时日柱算当天) + * + * @return 日天干,如甲 + */ + public String getDayGanExact2(){ + return LunarUtil.GAN[dayGanIndexExact2+1]; + } + /** * 获取日地支 * @@ -843,7 +869,7 @@ public class Lunar{ } /** - * 获取日地支(晚子时算第二天) + * 获取日地支(晚子时日柱算明天) * * @return 日地支,如卯 */ @@ -851,6 +877,15 @@ public class Lunar{ return LunarUtil.ZHI[dayZhiIndexExact+1]; } + /** + * 获取日地支(晚子时日柱算当天) + * + * @return 日地支,如卯 + */ + public String getDayZhiExact2(){ + return LunarUtil.ZHI[dayZhiIndexExact2+1]; + } + /** * 获取年生肖 * @@ -2308,10 +2343,18 @@ public class Lunar{ return dayGanIndexExact; } + public int getDayGanIndexExact2() { + return dayGanIndexExact2; + } + public int getDayZhiIndexExact() { return dayZhiIndexExact; } + public int getDayZhiIndexExact2() { + return dayZhiIndexExact2; + } + public int getMonthGanIndexExact() { return monthGanIndexExact; } @@ -2479,13 +2522,21 @@ public class Lunar{ } /** - * 获取日所在旬(晚子时算第二天) + * 获取日所在旬(晚子时日柱算明天) * @return 旬 */ public String getDayXunExact(){ return LunarUtil.getXun(getDayInGanZhiExact()); } + /** + * 获取日所在旬(晚子时日柱算当天) + * @return 旬 + */ + public String getDayXunExact2(){ + return LunarUtil.getXun(getDayInGanZhiExact2()); + } + /** * 获取值日空亡 * @return 空亡(旬空) @@ -2495,13 +2546,21 @@ public class Lunar{ } /** - * 获取值日空亡(晚子时算第二天) + * 获取值日空亡(晚子时日柱算明天) * @return 空亡(旬空) */ public String getDayXunKongExact(){ return LunarUtil.getXunKong(getDayInGanZhiExact()); } + /** + * 获取值日空亡(晚子时日柱算当天) + * @return 空亡(旬空) + */ + public String getDayXunKongExact2(){ + return LunarUtil.getXunKong(getDayInGanZhiExact2()); + } + /** * 获取时辰所在旬 * @return 旬 diff --git a/src/main/java/com/nlf/calendar/Solar.java b/src/main/java/com/nlf/calendar/Solar.java index f22b52c..9af2161 100644 --- a/src/main/java/com/nlf/calendar/Solar.java +++ b/src/main/java/com/nlf/calendar/Solar.java @@ -208,7 +208,7 @@ public class Solar{ } /** - * 通过八字获取阳历列表 + * 通过八字获取阳历列表(晚子时日柱按当天) * @param yearGanZhi 年柱 * @param monthGanZhi 月柱 * @param dayGanZhi 日柱 @@ -216,6 +216,20 @@ public class Solar{ * @return 符合的阳历列表 */ public static List 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 fromBaZi(String yearGanZhi,String monthGanZhi,String dayGanZhi,String timeGanZhi,int sect){ + sect = (1==sect)?1:2; List l = new ArrayList(); Solar today = new Solar(); Lunar lunar = today.getLunar(); @@ -271,7 +285,8 @@ public class Solar{ Solar solar = new Solar(year, month, day, hour, 0, 0); while (counter < 61) { 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); break; } diff --git a/src/test/java/test/BaZiTest.java b/src/test/java/test/BaZiTest.java index f4f7af0..0c98412 100644 --- a/src/test/java/test/BaZiTest.java +++ b/src/test/java/test/BaZiTest.java @@ -29,6 +29,12 @@ public class BaZiTest { EightChar eightChar = lunar.getEightChar(); Assert.assertEquals("年柱", "戊辰", eightChar.getYear()); 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.getTime());