From 221d714dca5b36a56f0277d20d3d4f5e53af316a Mon Sep 17 00:00:00 2001 From: 6tail <6tail@6tail.cn> Date: Tue, 28 Jul 2020 23:11:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=8ELunar=E4=B8=AD=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E6=8F=90=E5=87=BA=E5=85=AB=E5=AD=97(EightChar)=EF=BC=9B?= =?UTF-8?q?=E5=85=AB=E5=AD=97=E5=A2=9E=E5=8A=A0=E8=83=8E=E5=85=83=E3=80=81?= =?UTF-8?q?=E5=91=BD=E5=AE=AB=E3=80=81=E8=BA=AB=E5=AE=AB=EF=BC=9B=E5=85=AB?= =?UTF-8?q?=E5=AD=97=E5=A2=9E=E5=8A=A0=E5=9C=B0=E5=8A=BF=EF=BC=88=E9=95=BF?= =?UTF-8?q?=E7=94=9F=E5=8D=81=E4=BA=8C=E7=A5=9E=EF=BC=89=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/nlf/calendar/EightChar.java | 456 ++++++++++++++++++ src/main/java/com/nlf/calendar/Lunar.java | 144 ++++-- src/test/java/sample/BaZiTest.java | 3 + src/test/java/sample/BaZiTestNew.java | 132 +++++ src/test/java/test/BaZiTest.java | 108 +++++ 5 files changed, 791 insertions(+), 52 deletions(-) create mode 100644 src/main/java/com/nlf/calendar/EightChar.java create mode 100644 src/test/java/sample/BaZiTestNew.java create mode 100644 src/test/java/test/BaZiTest.java diff --git a/src/main/java/com/nlf/calendar/EightChar.java b/src/main/java/com/nlf/calendar/EightChar.java new file mode 100644 index 0000000..d250462 --- /dev/null +++ b/src/main/java/com/nlf/calendar/EightChar.java @@ -0,0 +1,456 @@ +package com.nlf.calendar; + +import com.nlf.calendar.util.LunarUtil; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 八字 + * @author 6tail + */ +public class EightChar { + /** 月支,按正月起寅排列 */ + private static final String[] MONTH_ZHI = {"","寅","卯","辰","巳","午","未","申","酉","戌","亥","子","丑"}; + /** 长生十二神 */ + public static final String[] CHANG_SHENG = {"长生","沐浴","冠带","临官","帝旺","衰","病","死","墓","绝","胎","养"}; + /** 长生十二神日干偏移值,五阳干顺推,五阴干逆推 */ + private static final Map CHANG_SHENG_OFFSET = new HashMap(){ + private static final long serialVersionUID = 1; + { + //阳 + put("甲",11); + put("丙",2); + put("戊",2); + put("庚",5); + put("壬",8); + //阴 + put("乙",6); + put("丁",9); + put("己",9); + put("辛",0); + put("癸",3); + } + }; + + /** 阴历 */ + protected Lunar lunar; + + public EightChar(Lunar lunar){ + this.lunar = lunar; + } + + @Override + public String toString(){ + return getYear()+" "+getMonth()+" "+getDay()+" "+getTime(); + } + + /** + * 获取年柱 + * @return 年柱 + */ + public String getYear(){ + return lunar.getYearInGanZhiExact(); + } + + /** + * 获取年干 + * @return 天干 + */ + public String getYearGan(){ + return lunar.getYearGanExact(); + } + + /** + * 获取年支 + * @return 地支 + */ + public String getYearZhi(){ + return lunar.getYearZhiExact(); + } + + /** + * 获取年柱地支藏干,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 + * @return 天干 + */ + public List getYearHideGan(){ + return LunarUtil.ZHI_HIDE_GAN.get(getYearZhi()); + } + + /** + * 获取年柱五行 + * @return 五行 + */ + public String getYearWuXing(){ + return LunarUtil.WU_XING_GAN.get(lunar.getYearGanExact())+LunarUtil.WU_XING_ZHI.get(lunar.getYearZhiExact()); + } + + /** + * 获取年柱纳音 + * @return 纳音 + */ + public String getYearNaYin(){ + return LunarUtil.NAYIN.get(getYear()); + } + + /** + * 获取年柱天干十神 + * @return 十神 + */ + public String getYearShiShenGan(){ + return LunarUtil.SHI_SHEN_GAN.get(getDayGan()+getYearGan()); + } + + private List getYearShiShenZhi(String zhi){ + List hideGan = LunarUtil.ZHI_HIDE_GAN.get(zhi); + List l = new ArrayList(hideGan.size()); + for(String gan:hideGan){ + l.add(LunarUtil.SHI_SHEN_ZHI.get(getDayGan()+zhi+gan)); + } + return l; + } + + /** + * 获取年柱地支十神,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 + * @return 十神 + */ + public List getYearShiShenZhi(){ + return getYearShiShenZhi(getYearZhi()); + } + + private String getDiShi(int zhiIndex){ + int offset = CHANG_SHENG_OFFSET.get(getDayGan()); + int index = offset + (lunar.getDayGanIndexExact()%2==0?zhiIndex:-zhiIndex); + if(index>=12){ + index -= 12; + } + if(index<0){ + index += 12; + } + return CHANG_SHENG[index]; + } + + /** + * 获取年柱地势(长生十二神) + * @return 地势 + */ + public String getYearDiShi(){ + return getDiShi(lunar.getYearZhiIndexExact()); + } + + /** + * 获取月柱 + * @return 月柱 + */ + public String getMonth(){ + return lunar.getMonthInGanZhiExact(); + } + + /** + * 获取月干 + * @return 天干 + */ + public String getMonthGan(){ + return lunar.getMonthGanExact(); + } + + /** + * 获取月支 + * @return 地支 + */ + public String getMonthZhi(){ + return lunar.getMonthZhiExact(); + } + + /** + * 获取月柱地支藏干,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 + * @return 天干 + */ + public List getMonthHideGan(){ + return LunarUtil.ZHI_HIDE_GAN.get(getMonthZhi()); + } + + /** + * 获取月柱五行 + * @return 五行 + */ + public String getMonthWuXing(){ + return LunarUtil.WU_XING_GAN.get(lunar.getMonthGanExact())+LunarUtil.WU_XING_ZHI.get(lunar.getMonthZhiExact()); + } + + /** + * 获取月柱纳音 + * @return 纳音 + */ + public String getMonthNaYin(){ + return LunarUtil.NAYIN.get(getMonth()); + } + + /** + * 获取月柱天干十神 + * @return 十神 + */ + public String getMonthShiShenGan(){ + return LunarUtil.SHI_SHEN_GAN.get(getDayGan()+getMonthGan()); + } + + /** + * 获取月柱地支十神,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 + * @return 十神 + */ + public List getMonthShiShenZhi(){ + return getYearShiShenZhi(getMonthZhi()); + } + + /** + * 获取月柱地势(长生十二神) + * @return 地势 + */ + public String getMonthDiShi(){ + return getDiShi(lunar.getMonthZhiIndexExact()); + } + + /** + * 获取日柱 + * @return 日柱 + */ + public String getDay(){ + return lunar.getDayInGanZhiExact(); + } + + /** + * 获取日干 + * @return 天干 + */ + public String getDayGan(){ + return lunar.getDayGanExact(); + } + + /** + * 获取日支 + * @return 地支 + */ + public String getDayZhi(){ + return lunar.getDayZhiExact(); + } + + /** + * 获取日柱地支藏干,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 + * @return 天干 + */ + public List getDayHideGan(){ + return LunarUtil.ZHI_HIDE_GAN.get(getDayZhi()); + } + + /** + * 获取日柱五行 + * @return 五行 + */ + public String getDayWuXing(){ + return LunarUtil.WU_XING_GAN.get(lunar.getDayGanExact())+LunarUtil.WU_XING_ZHI.get(lunar.getDayZhiExact()); + } + + /** + * 获取日柱纳音 + * @return 纳音 + */ + public String getDayNaYin(){ + return LunarUtil.NAYIN.get(getDay()); + } + + /** + * 获取日柱天干十神,也称日元、日干 + * @return 十神 + */ + public String getDayShiShenGan(){ + return "日主"; + } + + /** + * 获取日柱地支十神,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 + * @return 十神 + */ + public List getDayShiShenZhi(){ + return getYearShiShenZhi(getDayZhi()); + } + + /** + * 获取日柱地势(长生十二神) + * @return 地势 + */ + public String getDayDiShi(){ + return getDiShi(lunar.getDayZhiIndexExact()); + } + + /** + * 获取时柱 + * @return 时柱 + */ + public String getTime(){ + return lunar.getTimeInGanZhi(); + } + + /** + * 获取时干 + * @return 天干 + */ + public String getTimeGan(){ + return lunar.getTimeGan(); + } + + /** + * 获取时支 + * @return 地支 + */ + public String getTimeZhi(){ + return lunar.getTimeZhi(); + } + + /** + * 获取时柱地支藏干,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 + * @return 天干 + */ + public List getTimeHideGan(){ + return LunarUtil.ZHI_HIDE_GAN.get(getTimeZhi()); + } + + /** + * 获取时柱五行 + * @return 五行 + */ + public String getTimeWuXing(){ + return LunarUtil.WU_XING_GAN.get(lunar.getTimeGan())+LunarUtil.WU_XING_ZHI.get(lunar.getTimeZhi()); + } + + /** + * 获取时柱纳音 + * @return 纳音 + */ + public String getTimeNaYin(){ + return LunarUtil.NAYIN.get(getTime()); + } + + /** + * 获取时柱天干十神 + * @return 十神 + */ + public String getTimeShiShenGan(){ + return LunarUtil.SHI_SHEN_GAN.get(getDayGan()+getTimeGan()); + } + + /** + * 获取时柱地支十神,由于藏干分主气、余气、杂气,所以返回结果可能为1到3个元素 + * @return 十神 + */ + public List getTimeShiShenZhi(){ + return getYearShiShenZhi(getTimeZhi()); + } + + /** + * 获取时柱地势(长生十二神) + * @return 地势 + */ + public String getTimeDiShi(){ + return getDiShi(lunar.getTimeZhiIndex()); + } + + /** + * 获取胎元 + * @return 胎元 + */ + public String getTaiYuan(){ + int ganIndex = lunar.getMonthGanIndexExact() + 1; + if(ganIndex>10){ + ganIndex -= 10; + } + int zhiIndex = lunar.getMonthZhiIndexExact() + 3; + if(zhiIndex>12){ + zhiIndex -= 12; + } + return LunarUtil.GAN[ganIndex+1]+LunarUtil.ZHI[zhiIndex+1]; + } + + /** + * 获取胎元纳音 + * @return 纳音 + */ + public String getTaiYuanNaYin(){ + return LunarUtil.NAYIN.get(getTaiYuan()); + } + + /** + * 获取命宫 + * @return 命宫 + */ + public String getMingGong(){ + int monthZhiIndex = 0; + int timeZhiIndex = 0; + for(int i=0,j=MONTH_ZHI.length;i12){ + zhiIndex -= 12; + } + int jiaZiIndex = LunarUtil.getJiaZiIndex(lunar.getMonthInGanZhiExact()) - (monthZhiIndex-zhiIndex); + if(jiaZiIndex>=60){ + jiaZiIndex -= 60; + } + if(jiaZiIndex<0){ + jiaZiIndex += 60; + } + return LunarUtil.JIA_ZI[jiaZiIndex]; + } + + /** + * 获取命宫纳音 + * @return 纳音 + */ + public String getMingGongNaYin(){ + return LunarUtil.NAYIN.get(getMingGong()); + } + + /** + * 获取身宫 + * @return 身宫 + */ + public String getShenGong(){ + int monthZhiIndex = 0; + int timeZhiIndex = 0; + for(int i=0,j=MONTH_ZHI.length;i=60){ + jiaZiIndex -= 60; + } + if(jiaZiIndex<0){ + jiaZiIndex += 60; + } + return LunarUtil.JIA_ZI[jiaZiIndex]; + } + + /** + * 获取身宫纳音 + * @return 纳音 + */ + public String getShenGongNaYin(){ + return LunarUtil.NAYIN.get(getShenGong()); + } + +} diff --git a/src/main/java/com/nlf/calendar/Lunar.java b/src/main/java/com/nlf/calendar/Lunar.java index 9609467..4677012 100644 --- a/src/main/java/com/nlf/calendar/Lunar.java +++ b/src/main/java/com/nlf/calendar/Lunar.java @@ -72,6 +72,8 @@ public class Lunar{ private int minute; /** 阳历秒钟 */ private int second; + /** 八字 */ + private EightChar eightChar; /** 24节气表(对应阳历的准确时刻) */ private Map jieQi = new LinkedHashMap(); @@ -1436,122 +1438,121 @@ public class Lunar{ /** * 获取八字,男性也称乾造,女性也称坤造(以立春交接时刻作为新年的开始) * @return 八字(男性也称乾造,女性也称坤造) + * @deprecated 使用getEightChar */ + @Deprecated public List getBaZi(){ List l = new ArrayList(4); - l.add(getYearInGanZhiExact()); - l.add(getMonthInGanZhiExact()); - l.add(getDayInGanZhiExact()); - l.add(getTimeInGanZhi()); + EightChar eightChar = getEightChar(); + l.add(eightChar.getYear()); + l.add(eightChar.getMonth()); + l.add(eightChar.getDay()); + l.add(eightChar.getTime()); return l; } /** * 获取八字五行 * @return 八字五行 + * @deprecated 使用getEightChar */ + @Deprecated public List getBaZiWuXing(){ - List baZi = getBaZi(); - List l = new ArrayList(baZi.size()); - for(String ganZhi:baZi){ - String gan = ganZhi.substring(0,1); - String zhi = ganZhi.substring(1); - l.add(LunarUtil.WU_XING_GAN.get(gan)+LunarUtil.WU_XING_ZHI.get(zhi)); - } + List l = new ArrayList(4); + EightChar eightChar = getEightChar(); + l.add(eightChar.getYearWuXing()); + l.add(eightChar.getMonthWuXing()); + l.add(eightChar.getDayWuXing()); + l.add(eightChar.getTimeWuXing()); return l; } /** * 获取八字纳音 * @return 八字纳音 + * @deprecated 使用getEightChar */ + @Deprecated public List getBaZiNaYin(){ - List baZi = getBaZi(); - List l = new ArrayList(baZi.size()); - for(String ganZhi:baZi){ - l.add(LunarUtil.NAYIN.get(ganZhi)); - } + List l = new ArrayList(4); + EightChar eightChar = getEightChar(); + l.add(eightChar.getYearNaYin()); + l.add(eightChar.getMonthNaYin()); + l.add(eightChar.getDayNaYin()); + l.add(eightChar.getTimeNaYin()); return l; } /** * 获取八字天干十神,日柱十神为日主,其余三柱根据天干十神表查询 * @return 八字天干十神 + * @deprecated 使用getEightChar */ + @Deprecated public List getBaZiShiShenGan(){ - List baZi = getBaZi(); - String yearGan = baZi.get(0).substring(0,1); - String monthGan = baZi.get(1).substring(0,1); - String dayGan = baZi.get(2).substring(0,1); - String timeGan = baZi.get(3).substring(0,1); - List l = new ArrayList(baZi.size()); - l.add(LunarUtil.SHI_SHEN_GAN.get(dayGan+yearGan)); - l.add(LunarUtil.SHI_SHEN_GAN.get(dayGan+monthGan)); - l.add("日主"); - l.add(LunarUtil.SHI_SHEN_GAN.get(dayGan+timeGan)); + List l = new ArrayList(4); + EightChar eightChar = getEightChar(); + l.add(eightChar.getYearShiShenGan()); + l.add(eightChar.getMonthShiShenGan()); + l.add(eightChar.getDayShiShenGan()); + l.add(eightChar.getTimeShiShenGan()); return l; } /** * 获取八字地支十神,根据地支十神表查询 * @return 八字地支十神 + * @deprecated 使用getEightChar */ + @Deprecated public List getBaZiShiShenZhi(){ - List baZi = getBaZi(); - String dayGan = baZi.get(2).substring(0,1); - List l = new ArrayList(baZi.size()); - for(String ganZhi:baZi){ - String zhi = ganZhi.substring(1); - l.add(LunarUtil.SHI_SHEN_ZHI.get(dayGan+zhi+LunarUtil.ZHI_HIDE_GAN.get(zhi).get(0))); - } - return l; - } - - private List getBaZiShiShenZhi(String zhi){ - List baZi = getBaZi(); - String dayGan = baZi.get(2).substring(0,1); - List hideGan = LunarUtil.ZHI_HIDE_GAN.get(zhi); - List l = new ArrayList(hideGan.size()); - for(String gan:hideGan){ - l.add(LunarUtil.SHI_SHEN_ZHI.get(dayGan+zhi+gan)); - } + List l = new ArrayList(4); + EightChar eightChar = getEightChar(); + l.add(eightChar.getYearShiShenZhi().get(0)); + l.add(eightChar.getMonthShiShenZhi().get(0)); + l.add(eightChar.getDayShiShenZhi().get(0)); + l.add(eightChar.getTimeShiShenZhi().get(0)); return l; } /** * 获取八字年支十神 * @return 八字年支十神 + * @deprecated 使用getEightChar */ + @Deprecated public List getBaZiShiShenYearZhi(){ - List baZi = getBaZi(); - return getBaZiShiShenZhi(baZi.get(0).substring(1)); + return getEightChar().getYearShiShenZhi(); } /** * 获取八字月支十神 * @return 八字月支十神 + * @deprecated 使用getEightChar */ + @Deprecated public List getBaZiShiShenMonthZhi(){ - List baZi = getBaZi(); - return getBaZiShiShenZhi(baZi.get(1).substring(1)); + return getEightChar().getMonthShiShenZhi(); } /** * 获取八字日支十神 * @return 八字日支十神 + * @deprecated 使用getEightChar */ + @Deprecated public List getBaZiShiShenDayZhi(){ - List baZi = getBaZi(); - return getBaZiShiShenZhi(baZi.get(2).substring(1)); + return getEightChar().getDayShiShenZhi(); } /** * 获取八字时支十神 * @return 八字时支十神 + * @deprecated 使用getEightChar */ + @Deprecated public List getBaZiShiShenTimeZhi(){ - List baZi = getBaZi(); - return getBaZiShiShenZhi(baZi.get(3).substring(1)); + return getEightChar().getTimeShiShenZhi(); } /** @@ -2060,7 +2061,46 @@ public class Lunar{ return second; } + public int getTimeGanIndex() { + return timeGanIndex; + } + + public int getTimeZhiIndex() { + return timeZhiIndex; + } + + public int getDayGanIndexExact() { + return dayGanIndexExact; + } + + public int getDayZhiIndexExact() { + return dayZhiIndexExact; + } + + public int getMonthGanIndexExact() { + return monthGanIndexExact; + } + + public int getMonthZhiIndexExact() { + return monthZhiIndexExact; + } + + public int getYearGanIndexExact(){ + return yearGanIndexExact; + } + + public int getYearZhiIndexExact(){ + return yearZhiIndexExact; + } + public Solar getSolar(){ return solar; } + + public EightChar getEightChar(){ + if(null==eightChar){ + eightChar = new EightChar(this); + } + return eightChar; + } } diff --git a/src/test/java/sample/BaZiTest.java b/src/test/java/sample/BaZiTest.java index fc629e5..2b41c8e 100644 --- a/src/test/java/sample/BaZiTest.java +++ b/src/test/java/sample/BaZiTest.java @@ -8,7 +8,10 @@ import java.util.List; /** * 八字测试 + * @author 6tail + * @deprecated 见BaZiTestNew */ +@Deprecated public class BaZiTest { @Test diff --git a/src/test/java/sample/BaZiTestNew.java b/src/test/java/sample/BaZiTestNew.java new file mode 100644 index 0000000..e3e5b0b --- /dev/null +++ b/src/test/java/sample/BaZiTestNew.java @@ -0,0 +1,132 @@ +package sample; + +import com.nlf.calendar.Lunar; +import com.nlf.calendar.Solar; +import org.junit.Test; + +import java.util.List; + +/** + * 八字测试 + * @author 6tail + */ +public class BaZiTestNew { + + @Test + public void test1(){ + Solar solar = new Solar(2020,1,1,22,35,0); + Lunar lunar = solar.getLunar(); + //己亥 丙子 癸卯 癸亥 + System.out.println(lunar.getEightChar()); + } + + @Test + public void test2(){ + Solar solar = new Solar(2020,1,6,14,35,0); + Lunar lunar = solar.getLunar(); + //己亥, 丁丑, 戊申, 己未] + System.out.println(lunar.getEightChar()); + } + + @Test + public void test3(){ + Solar solar = new Solar(2020,1,6,3,35,0); + Lunar lunar = solar.getLunar(); + //己亥, 丁丑, 戊辰, 癸亥] + System.out.println(lunar.getEightChar()); + } + + @Test + public void test4(){ + Solar solar = new Solar(2020,1,26,21,41,0); + Lunar lunar = solar.getLunar(); + //己亥, 丙子, 戊申, 甲寅] + System.out.println(lunar.getEightChar()); + } + + @Test + public void test5(){ + Solar solar = new Solar(2020,2,4,1,42,0); + Lunar lunar = solar.getLunar(); + //己亥, 丁丑, 丁丑, 辛丑] + System.out.println(lunar.getEightChar()); + } + + @Test + public void test6(){ + Solar solar = new Solar(2020,2,5,21,43,0); + Lunar lunar = solar.getLunar(); + //庚子, 戊寅, 戊寅, 癸亥] + System.out.println(lunar.getEightChar()); + } + + @Test + public void testBazi2Solar6() { + List l = Solar.fromBaZi("庚子", "戊寅", "戊寅", "癸亥"); + // [2020-02-05 22:00:00, 1960-02-20 22:00:00] + for (Solar solar : l) { + System.out.println(solar.toFullString()); + } + } + + @Test + public void test7(){ + Solar solar = new Solar(2020,5,26,23,43,0); + Lunar lunar = solar.getLunar(); + //庚子, 辛巳, 庚午, 丙子] + System.out.println(lunar.getEightChar()); + } + + @Test + public void testBazi2Solar7() { + List l = Solar.fromBaZi("庚子", "辛巳", "庚午", "丙子"); + // [2020-05-26 23:00:00, 2020-05-27 00:00:00] + for (Solar solar : l) { + System.out.println(solar.toFullString()); + } + } + + @Test + public void testBazi2Solar() { + List l = Solar.fromBaZi("庚子", "癸未", "乙丑", "丁亥"); + // [2020-07-21 22:00:00, 1960-08-05 22:00:00] + for (Solar solar : l) { + System.out.println(solar.toFullString()); + } + } + + @Test + public void testBazi2Solar2() { + List l = Solar.fromBaZi("庚子", "戊子", "己卯", "庚午"); + // [1960-12-17 12:00:00, 1901-01-01 12:00:00] + for (Solar solar : l) { + System.out.println(solar.toFullString()); + } + } + + @Test + public void testBaziShiShenZhi() { + Solar solar = new Solar(2020,1,1,22,35,0); + Lunar lunar = solar.getLunar(); + //己亥 丙子 癸卯 癸亥 + System.out.println(lunar.getEightChar()); + //七杀 + System.out.println(lunar.getEightChar().getYearShiShenGan()); + //正财 + System.out.println(lunar.getEightChar().getMonthShiShenGan()); + //日主 + System.out.println(lunar.getEightChar().getDayShiShenGan()); + //比肩 + System.out.println(lunar.getEightChar().getTimeShiShenGan()); + + //[劫财, 伤官] + System.out.println(lunar.getEightChar().getYearShiShenZhi()); + //[比肩] + System.out.println(lunar.getEightChar().getMonthShiShenZhi()); + //[食神] + System.out.println(lunar.getEightChar().getDayShiShenZhi()); + //[劫财, 伤官] + System.out.println(lunar.getEightChar().getTimeShiShenZhi()); + } + +} diff --git a/src/test/java/test/BaZiTest.java b/src/test/java/test/BaZiTest.java new file mode 100644 index 0000000..7177246 --- /dev/null +++ b/src/test/java/test/BaZiTest.java @@ -0,0 +1,108 @@ +package test; + +import com.nlf.calendar.EightChar; +import com.nlf.calendar.Lunar; +import com.nlf.calendar.Solar; +import org.junit.Assert; +import org.junit.Test; + +/** + * 八字测试 + */ +public class BaZiTest { + + @Test + public void testGanZhi() { + Solar solar = new Solar(2005, 12, 23, 8, 37, 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()); + } + + @Test + public void testHideGan() { + Solar solar = new Solar(2005, 12, 23, 8, 37, 0); + Lunar lunar = solar.getLunar(); + EightChar eightChar = lunar.getEightChar(); + Assert.assertEquals("年支藏干", "[辛]", eightChar.getYearHideGan().toString()); + Assert.assertEquals("月支藏干", "[癸]", eightChar.getMonthHideGan().toString()); + Assert.assertEquals("日支藏干", "[丙, 庚, 戊]", eightChar.getDayHideGan().toString()); + Assert.assertEquals("时支藏干", "[戊, 乙, 癸]", eightChar.getTimeHideGan().toString()); + } + + @Test + public void testShiShenGan() { + Solar solar = new Solar(2005, 12, 23, 8, 37, 0); + Lunar lunar = solar.getLunar(); + EightChar eightChar = lunar.getEightChar(); + Assert.assertEquals("年干十神", "偏财", eightChar.getYearShiShenGan()); + Assert.assertEquals("月干十神", "正印", eightChar.getMonthShiShenGan()); + Assert.assertEquals("日干十神", "日主", eightChar.getDayShiShenGan()); + Assert.assertEquals("时干十神", "伤官", eightChar.getTimeShiShenGan()); + } + + @Test + public void testShiShenZhi() { + Solar solar = new Solar(2005, 12, 23, 8, 37, 0); + Lunar lunar = solar.getLunar(); + EightChar eightChar = lunar.getEightChar(); + Assert.assertEquals("年支十神", "[比肩]", eightChar.getYearShiShenZhi().toString()); + Assert.assertEquals("月支十神", "[食神]", eightChar.getMonthShiShenZhi().toString()); + Assert.assertEquals("日支十神", "[正官, 劫财, 正印]", eightChar.getDayShiShenZhi().toString()); + Assert.assertEquals("时支十神", "[正印, 偏财, 食神]", eightChar.getTimeShiShenZhi().toString()); + } + + @Test + public void testDiShi() { + Solar solar = new Solar(2005, 12, 23, 8, 37, 0); + Lunar lunar = solar.getLunar(); + EightChar eightChar = lunar.getEightChar(); + Assert.assertEquals("年柱地势", "临官", eightChar.getYearDiShi()); + Assert.assertEquals("月柱地势", "长生", eightChar.getMonthDiShi()); + Assert.assertEquals("日柱地势", "死", eightChar.getDayDiShi()); + Assert.assertEquals("时柱地势", "墓", eightChar.getTimeDiShi()); + } + + @Test + public void testNaYin() { + Solar solar = new Solar(2005, 12, 23, 8, 37, 0); + Lunar lunar = solar.getLunar(); + EightChar eightChar = lunar.getEightChar(); + Assert.assertEquals("年柱纳音", "泉中水", eightChar.getYearNaYin()); + Assert.assertEquals("月柱纳音", "霹雳火", eightChar.getMonthNaYin()); + Assert.assertEquals("日柱纳音", "白蜡金", eightChar.getDayNaYin()); + Assert.assertEquals("时柱纳音", "长流水", eightChar.getTimeNaYin()); + } + + @Test + public void testTaiYuan() { + Solar solar = new Solar(2005, 12, 23, 8, 37, 0); + Lunar lunar = solar.getLunar(); + Assert.assertEquals("胎元", "己卯", lunar.getEightChar().getTaiYuan()); + + lunar = new Solar(1995, 12, 18, 10, 28, 0).getLunar(); + Assert.assertEquals("胎元", "己卯", lunar.getEightChar().getTaiYuan()); + } + + @Test + public void testMingGong() { + Lunar lunar = new Solar(2005, 12, 23, 8, 37, 0).getLunar(); + Assert.assertEquals("命宫", "己丑", lunar.getEightChar().getMingGong()); + + lunar = new Solar(1998, 6, 11, 4, 28, 0).getLunar(); + Assert.assertEquals("命宫", "辛酉", lunar.getEightChar().getMingGong()); + + lunar = new Solar(1995, 12, 18, 10, 28, 0).getLunar(); + Assert.assertEquals("命宫", "戊子", lunar.getEightChar().getMingGong()); + } + + @Test + public void testShenGong() { + Lunar lunar = new Solar(1995, 12, 18, 10, 28, 0).getLunar(); + Assert.assertEquals("身宫", "壬午", lunar.getEightChar().getShenGong()); + } + +}