From 135d39b751407c8b93fc5cb73d527181e9ce0fba Mon Sep 17 00:00:00 2001 From: 6tail <6tail@6tail.cn> Date: Thu, 19 Nov 2020 12:43:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=AC=E3=80=81=E6=97=AC?= =?UTF-8?q?=E7=A9=BA(=E7=A9=BA=E4=BA=A1)=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 | 62 ++++++ src/main/java/com/nlf/calendar/Lunar.java | 128 ++++++++++++ .../com/nlf/calendar/eightchar/DaYun.java | 16 ++ .../com/nlf/calendar/eightchar/LiuNian.java | 20 ++ .../com/nlf/calendar/eightchar/LiuYue.java | 16 ++ .../com/nlf/calendar/eightchar/XiaoYun.java | 16 ++ .../java/com/nlf/calendar/util/LunarUtil.java | 51 +++++ src/test/java/test/XunTest.java | 182 ++++++++++++++++++ 8 files changed, 491 insertions(+) create mode 100644 src/test/java/test/XunTest.java diff --git a/src/main/java/com/nlf/calendar/EightChar.java b/src/main/java/com/nlf/calendar/EightChar.java index 4721e47..99e46b6 100644 --- a/src/main/java/com/nlf/calendar/EightChar.java +++ b/src/main/java/com/nlf/calendar/EightChar.java @@ -524,5 +524,67 @@ public Yun getYun(int gender) { return new Yun(this, gender); } + /** + * 获取年柱所在旬 + * @return 旬 + */ + public String getYearXun(){ + return lunar.getYearXunExact(); + } + + /** + * 获取年柱旬空(空亡) + * @return 旬空(空亡) + */ + public String getYearXunKong(){ + return lunar.getYearXunKongExact(); + } + + /** + * 获取月柱所在旬 + * @return 旬 + */ + public String getMonthXun(){ + return lunar.getMonthXunExact(); + } + + /** + * 获取月柱旬空(空亡) + * @return 旬空(空亡) + */ + public String getMonthXunKong(){ + return lunar.getMonthXunKongExact(); + } + /** + * 获取日柱所在旬 + * @return 旬 + */ + public String getDayXun(){ + return lunar.getDayXunExact(); + } + + /** + * 获取日柱旬空(空亡) + * @return 旬空(空亡) + */ + public String getDayXunKong(){ + return lunar.getDayXunKongExact(); + } + + /** + * 获取时柱所在旬 + * @return 旬 + */ + public String getTimeXun(){ + return lunar.getTimeXun(); + } + + /** + * 获取时柱旬空(空亡) + * @return 旬空(空亡) + */ + 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 937a402..732fd78 100644 --- a/src/main/java/com/nlf/calendar/Lunar.java +++ b/src/main/java/com/nlf/calendar/Lunar.java @@ -2389,4 +2389,132 @@ public Lunar next(int days){ } return new Lunar(y,m,d,hour,minute,second); } + + /** + * 获取年所在旬(以正月初一作为新年的开始) + * @return 旬 + */ + public String getYearXun(){ + return LunarUtil.getXun(getYearInGanZhi()); + } + + /** + * 获取年所在旬(以立春当天作为新年的开始) + * @return 旬 + */ + public String getYearXunByLiChun(){ + return LunarUtil.getXun(getYearInGanZhiByLiChun()); + } + + /** + * 获取年所在旬(以立春交接时刻作为新年的开始) + * @return 旬 + */ + public String getYearXunExact(){ + return LunarUtil.getXun(getYearInGanZhiExact()); + } + + /** + * 获取值年空亡(以正月初一作为新年的开始) + * @return 空亡(旬空) + */ + public String getYearXunKong(){ + return LunarUtil.getXunKong(getYearInGanZhi()); + } + + /** + * 获取值年空亡(以立春当天作为新年的开始) + * @return 空亡(旬空) + */ + public String getYearXunKongByLiChun(){ + return LunarUtil.getXunKong(getYearInGanZhiByLiChun()); + } + + /** + * 获取值年空亡(以立春交接时刻作为新年的开始) + * @return 空亡(旬空) + */ + public String getYearXunKongExact(){ + return LunarUtil.getXunKong(getYearInGanZhiExact()); + } + + /** + * 获取月所在旬(以节交接当天起算) + * @return 旬 + */ + public String getMonthXun(){ + return LunarUtil.getXun(getMonthInGanZhi()); + } + + /** + * 获取月所在旬(以节交接时刻起算) + * @return 旬 + */ + public String getMonthXunExact(){ + return LunarUtil.getXun(getMonthInGanZhiExact()); + } + + /** + * 获取值月空亡(以节交接当天起算) + * @return 空亡(旬空) + */ + public String getMonthXunKong(){ + return LunarUtil.getXunKong(getMonthInGanZhi()); + } + + /** + * 获取值月空亡(以节交接时刻起算) + * @return 空亡(旬空) + */ + public String getMonthXunKongExact(){ + return LunarUtil.getXunKong(getMonthInGanZhiExact()); + } + + /** + * 获取日所在旬(以节交接当天起算) + * @return 旬 + */ + public String getDayXun(){ + return LunarUtil.getXun(getDayInGanZhi()); + } + + /** + * 获取日所在旬(晚子时算第二天) + * @return 旬 + */ + public String getDayXunExact(){ + return LunarUtil.getXun(getDayInGanZhiExact()); + } + + /** + * 获取值日空亡 + * @return 空亡(旬空) + */ + public String getDayXunKong(){ + return LunarUtil.getXunKong(getDayInGanZhi()); + } + + /** + * 获取值日空亡(晚子时算第二天) + * @return 空亡(旬空) + */ + public String getDayXunKongExact(){ + return LunarUtil.getXunKong(getDayInGanZhiExact()); + } + + /** + * 获取时辰所在旬 + * @return 旬 + */ + public String getTimeXun(){ + return LunarUtil.getXun(getTimeInGanZhi()); + } + + /** + * 获取值时空亡 + * @return 空亡(旬空) + */ + public String getTimeXunKong(){ + return LunarUtil.getXunKong(getTimeInGanZhi()); + } } diff --git a/src/main/java/com/nlf/calendar/eightchar/DaYun.java b/src/main/java/com/nlf/calendar/eightchar/DaYun.java index baac118..dc428c4 100644 --- a/src/main/java/com/nlf/calendar/eightchar/DaYun.java +++ b/src/main/java/com/nlf/calendar/eightchar/DaYun.java @@ -100,6 +100,22 @@ public String getGanZhi() { return LunarUtil.JIA_ZI[offset]; } + /** + * 获取所在旬 + * @return 旬 + */ + public String getXun(){ + return LunarUtil.getXun(getGanZhi()); + } + + /** + * 获取旬空(空亡) + * @return 旬空(空亡) + */ + public String getXunKong(){ + return LunarUtil.getXunKong(getGanZhi()); + } + /** * 获取流年 * diff --git a/src/main/java/com/nlf/calendar/eightchar/LiuNian.java b/src/main/java/com/nlf/calendar/eightchar/LiuNian.java index dbdbbd5..706d33f 100644 --- a/src/main/java/com/nlf/calendar/eightchar/LiuNian.java +++ b/src/main/java/com/nlf/calendar/eightchar/LiuNian.java @@ -61,6 +61,26 @@ public String getGanZhi() { return LunarUtil.JIA_ZI[offset]; } + /** + * 获取所在旬 + * @return 旬 + */ + public String getXun(){ + return LunarUtil.getXun(getGanZhi()); + } + + /** + * 获取旬空(空亡) + * @return 旬空(空亡) + */ + public String getXunKong(){ + return LunarUtil.getXunKong(getGanZhi()); + } + + /** + * 获取流月 + * @return 流月 + */ public LiuYue[] getLiuYue() { int n = 12; LiuYue[] l = new LiuYue[n]; diff --git a/src/main/java/com/nlf/calendar/eightchar/LiuYue.java b/src/main/java/com/nlf/calendar/eightchar/LiuYue.java index fb57dad..9298dd7 100644 --- a/src/main/java/com/nlf/calendar/eightchar/LiuYue.java +++ b/src/main/java/com/nlf/calendar/eightchar/LiuYue.java @@ -61,4 +61,20 @@ public String getGanZhi() { String zhi = LunarUtil.ZHI[(index + LunarUtil.BASE_MONTH_ZHI_INDEX) % 12 + 1]; return gan + zhi; } + + /** + * 获取所在旬 + * @return 旬 + */ + public String getXun(){ + return LunarUtil.getXun(getGanZhi()); + } + + /** + * 获取旬空(空亡) + * @return 旬空(空亡) + */ + public String getXunKong(){ + return LunarUtil.getXunKong(getGanZhi()); + } } diff --git a/src/main/java/com/nlf/calendar/eightchar/XiaoYun.java b/src/main/java/com/nlf/calendar/eightchar/XiaoYun.java index 49c1256..3b784cc 100644 --- a/src/main/java/com/nlf/calendar/eightchar/XiaoYun.java +++ b/src/main/java/com/nlf/calendar/eightchar/XiaoYun.java @@ -71,4 +71,20 @@ public String getGanZhi() { offset %= size; return LunarUtil.JIA_ZI[offset]; } + + /** + * 获取所在旬 + * @return 旬 + */ + public String getXun(){ + return LunarUtil.getXun(getGanZhi()); + } + + /** + * 获取旬空(空亡) + * @return 旬空(空亡) + */ + public String getXunKong(){ + return LunarUtil.getXunKong(getGanZhi()); + } } diff --git a/src/main/java/com/nlf/calendar/util/LunarUtil.java b/src/main/java/com/nlf/calendar/util/LunarUtil.java index 6626559..2a88638 100644 --- a/src/main/java/com/nlf/calendar/util/LunarUtil.java +++ b/src/main/java/com/nlf/calendar/util/LunarUtil.java @@ -31,6 +31,10 @@ public class LunarUtil{ public static final int[] LEAP_MONTH_YEAR = {6,14,19,25,33,36,38,41,44,52,55,79,117,136,147,150,155,158,185,193}; /** 闰月表 */ public static final int[] LUNAR_MONTH = {0x00,0x04,0xad,0x08,0x5a,0x01,0xd5,0x54,0xb4,0x09,0x64,0x05,0x59,0x45,0x95,0x0a,0xa6,0x04,0x55,0x24,0xad,0x08,0x5a,0x62,0xda,0x04,0xb4,0x05,0xb4,0x55,0x52,0x0d,0x94,0x0a,0x4a,0x2a,0x56,0x02,0x6d,0x71,0x6d,0x01,0xda,0x02,0xd2,0x52,0xa9,0x05,0x49,0x0d,0x2a,0x45,0x2b,0x09,0x56,0x01,0xb5,0x20,0x6d,0x01,0x59,0x69,0xd4,0x0a,0xa8,0x05,0xa9,0x56,0xa5,0x04,0x2b,0x09,0x9e,0x38,0xb6,0x08,0xec,0x74,0x6c,0x05,0xd4,0x0a,0xe4,0x6a,0x52,0x05,0x95,0x0a,0x5a,0x42,0x5b,0x04,0xb6,0x04,0xb4,0x22,0x6a,0x05,0x52,0x75,0xc9,0x0a,0x52,0x05,0x35,0x55,0x4d,0x0a,0x5a,0x02,0x5d,0x31,0xb5,0x02,0x6a,0x8a,0x68,0x05,0xa9,0x0a,0x8a,0x6a,0x2a,0x05,0x2d,0x09,0xaa,0x48,0x5a,0x01,0xb5,0x09,0xb0,0x39,0x64,0x05,0x25,0x75,0x95,0x0a,0x96,0x04,0x4d,0x54,0xad,0x04,0xda,0x04,0xd4,0x44,0xb4,0x05,0x54,0x85,0x52,0x0d,0x92,0x0a,0x56,0x6a,0x56,0x02,0x6d,0x02,0x6a,0x41,0xda,0x02,0xb2,0xa1,0xa9,0x05,0x49,0x0d,0x0a,0x6d,0x2a,0x09,0x56,0x01,0xad,0x50,0x6d,0x01,0xd9,0x02,0xd1,0x3a,0xa8,0x05,0x29,0x85,0xa5,0x0c,0x2a,0x09,0x96,0x54,0xb6,0x08,0x6c,0x09,0x64,0x45,0xd4,0x0a,0xa4,0x05,0x51,0x25,0x95,0x0a,0x2a,0x72,0x5b,0x04,0xb6,0x04,0xac,0x52,0x6a,0x05,0xd2,0x0a,0xa2,0x4a,0x4a,0x05,0x55,0x94,0x2d,0x0a,0x5a,0x02,0x75,0x61,0xb5,0x02,0x6a,0x03,0x61,0x45,0xa9,0x0a,0x4a,0x05,0x25,0x25,0x2d,0x09,0x9a,0x68,0xda,0x08,0xb4,0x09,0xa8,0x59,0x54,0x03,0xa5,0x0a,0x91,0x3a,0x96,0x04,0xad,0xb0,0xad,0x04,0xda,0x04,0xf4,0x62,0xb4,0x05,0x54,0x0b,0x44,0x5d,0x52,0x0a,0x95,0x04,0x55,0x22,0x6d,0x02,0x5a,0x71,0xda,0x02,0xaa,0x05,0xb2,0x55,0x49,0x0b,0x4a,0x0a,0x2d,0x39,0x36,0x01,0x6d,0x80,0x6d,0x01,0xd9,0x02,0xe9,0x6a,0xa8,0x05,0x29,0x0b,0x9a,0x4c,0xaa,0x08,0xb6,0x08,0xb4,0x38,0x6c,0x09,0x54,0x75,0xd4,0x0a,0xa4,0x05,0x45,0x55,0x95,0x0a,0x9a,0x04,0x55,0x44,0xb5,0x04,0x6a,0x82,0x6a,0x05,0xd2,0x0a,0x92,0x6a,0x4a,0x05,0x55,0x0a,0x2a,0x4a,0x5a,0x02,0xb5,0x02,0xb2,0x31,0x69,0x03,0x31,0x73,0xa9,0x0a,0x4a,0x05,0x2d,0x55,0x2d,0x09,0x5a,0x01,0xd5,0x48,0xb4,0x09,0x68,0x89,0x54,0x0b,0xa4,0x0a,0xa5,0x6a,0x95,0x04,0xad,0x08,0x6a,0x44,0xda,0x04,0x74,0x05,0xb0,0x25,0x54,0x03}; + /** 旬 */ + public static final String[] XUN = {"甲子","甲戌","甲申","甲午","甲辰","甲寅"}; + /** 旬空 */ + public static final String[] XUN_KONG = {"戌亥","申酉","午未","辰巳","寅卯","子丑"}; /** 天干 */ public static final String[] GAN = {"","甲","乙","丙","丁","戊","己","庚","辛","壬","癸"}; /** 喜神方位,《喜神方位歌》:甲己在艮乙庚乾,丙辛坤位喜神安.  丁壬只在离宫坐,戊癸原在在巽间。 */ @@ -1444,4 +1448,51 @@ public static List getTimeJi(String dayGanZhi,String timeGanZhi){ } return l; } + + /** + * 获取干支所在旬下标,0-5 + * @param ganZhi 干支 + * @return 旬下标,0-5 + */ + protected static int getXunIndex(String ganZhi){ + String gan = ganZhi.substring(0,1); + String zhi = ganZhi.substring(1); + int ganIndex = 0; + int zhiIndex = 0; + for(int i=0,j=GAN.length;i xun = new HashMap(); + xun.put("甲子","甲子"); + xun.put("乙丑","甲子"); + xun.put("丙寅","甲子"); + xun.put("丁卯","甲子"); + xun.put("戊辰","甲子"); + xun.put("己巳","甲子"); + xun.put("庚午","甲子"); + xun.put("辛未","甲子"); + xun.put("壬申","甲子"); + xun.put("癸酉","甲子"); + xun.put("甲戌","甲戌"); + xun.put("乙亥","甲戌"); + xun.put("丙子","甲戌"); + xun.put("丁丑","甲戌"); + xun.put("戊寅","甲戌"); + xun.put("己卯","甲戌"); + xun.put("庚辰","甲戌"); + xun.put("辛巳","甲戌"); + xun.put("壬午","甲戌"); + xun.put("癸未","甲戌"); + xun.put("甲申","甲申"); + xun.put("乙酉","甲申"); + xun.put("丙戌","甲申"); + xun.put("丁亥","甲申"); + xun.put("戊子","甲申"); + xun.put("己丑","甲申"); + xun.put("庚寅","甲申"); + xun.put("辛卯","甲申"); + xun.put("壬辰","甲申"); + xun.put("癸巳","甲申"); + xun.put("甲午","甲午"); + xun.put("乙未","甲午"); + xun.put("丙申","甲午"); + xun.put("丁酉","甲午"); + xun.put("戊戌","甲午"); + xun.put("己亥","甲午"); + xun.put("庚子","甲午"); + xun.put("辛丑","甲午"); + xun.put("壬寅","甲午"); + xun.put("癸卯","甲午"); + xun.put("甲辰","甲辰"); + xun.put("乙巳","甲辰"); + xun.put("丙午","甲辰"); + xun.put("丁未","甲辰"); + xun.put("戊申","甲辰"); + xun.put("己酉","甲辰"); + xun.put("庚戌","甲辰"); + xun.put("辛亥","甲辰"); + xun.put("壬子","甲辰"); + xun.put("癸丑","甲辰"); + xun.put("甲寅","甲寅"); + xun.put("乙卯","甲寅"); + xun.put("丙辰","甲寅"); + xun.put("丁巳","甲寅"); + xun.put("戊午","甲寅"); + xun.put("己未","甲寅"); + xun.put("庚申","甲寅"); + xun.put("辛酉","甲寅"); + xun.put("壬戌","甲寅"); + xun.put("癸亥","甲寅"); + + for(Map.Entry entry:xun.entrySet()){ + Assert.assertEquals(entry.getValue(), LunarUtil.getXun(entry.getKey())); + } + } + + @Test + public void testXunKong(){ + Map kong = new HashMap(); + kong.put("甲子","戌亥"); + kong.put("乙丑","戌亥"); + kong.put("丙寅","戌亥"); + kong.put("丁卯","戌亥"); + kong.put("戊辰","戌亥"); + kong.put("己巳","戌亥"); + kong.put("庚午","戌亥"); + kong.put("辛未","戌亥"); + kong.put("壬申","戌亥"); + kong.put("癸酉","戌亥"); + kong.put("甲戌","申酉"); + kong.put("乙亥","申酉"); + kong.put("丙子","申酉"); + kong.put("丁丑","申酉"); + kong.put("戊寅","申酉"); + kong.put("己卯","申酉"); + kong.put("庚辰","申酉"); + kong.put("辛巳","申酉"); + kong.put("壬午","申酉"); + kong.put("癸未","申酉"); + kong.put("甲申","午未"); + kong.put("乙酉","午未"); + kong.put("丙戌","午未"); + kong.put("丁亥","午未"); + kong.put("戊子","午未"); + kong.put("己丑","午未"); + kong.put("庚寅","午未"); + kong.put("辛卯","午未"); + kong.put("壬辰","午未"); + kong.put("癸巳","午未"); + kong.put("甲午","辰巳"); + kong.put("乙未","辰巳"); + kong.put("丙申","辰巳"); + kong.put("丁酉","辰巳"); + kong.put("戊戌","辰巳"); + kong.put("己亥","辰巳"); + kong.put("庚子","辰巳"); + kong.put("辛丑","辰巳"); + kong.put("壬寅","辰巳"); + kong.put("癸卯","辰巳"); + kong.put("甲辰","寅卯"); + kong.put("乙巳","寅卯"); + kong.put("丙午","寅卯"); + kong.put("丁未","寅卯"); + kong.put("戊申","寅卯"); + kong.put("己酉","寅卯"); + kong.put("庚戌","寅卯"); + kong.put("辛亥","寅卯"); + kong.put("壬子","寅卯"); + kong.put("癸丑","寅卯"); + kong.put("甲寅","子丑"); + kong.put("乙卯","子丑"); + kong.put("丙辰","子丑"); + kong.put("丁巳","子丑"); + kong.put("戊午","子丑"); + kong.put("己未","子丑"); + kong.put("庚申","子丑"); + kong.put("辛酉","子丑"); + kong.put("壬戌","子丑"); + kong.put("癸亥","子丑"); + + for(Map.Entry entry:kong.entrySet()){ + Assert.assertEquals(entry.getValue(), LunarUtil.getXunKong(entry.getKey())); + } + } + + @Test + public void testXun1(){ + Solar solar = new Solar(2020,11,19,0,0,0); + Lunar lunar = solar.getLunar(); + Assert.assertEquals("甲午",lunar.getYearXun()); + } + + @Test + public void testXunKong1(){ + Solar solar = new Solar(2020,11,19,0,0,0); + Lunar lunar = solar.getLunar(); + Assert.assertEquals("辰巳",lunar.getYearXunKong()); + Assert.assertEquals("午未",lunar.getMonthXunKong()); + Assert.assertEquals("戌亥",lunar.getDayXunKong()); + } + + @Test + public void testBaZiDayXunKong(){ + Solar solar = new Solar(1990,12,23,8,37,0); + Lunar lunar = solar.getLunar(); + EightChar eightChar = lunar.getEightChar(); + Assert.assertEquals("子丑",eightChar.getDayXunKong()); + } + +}