From 3ee30dc1cc3a6a8c9c70474ef808758297cc2fe8 Mon Sep 17 00:00:00 2001 From: 6tail <6tail@6tail.cn> Date: Tue, 26 May 2020 23:12:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=B6=E8=BE=B0?= =?UTF-8?q?=E5=A4=A9=E5=B9=B2=E8=AE=A1=E7=AE=97=E9=94=99=E8=AF=AF=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=97=A9=E5=AD=90=E6=97=B6=E5=92=8C=E6=99=9A?= =?UTF-8?q?=E5=AD=90=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- README_EN.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 44fd7d8..f5467c6 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ lunar是一款无第三方依赖的公历(阳历)和农历(阴历、老黄历) 输出结果: - 壹玖捌陆年肆月廿一 丙寅(虎)年 癸巳(蛇)月 癸酉(鸡)日 子(鼠)时 纳音[炉中火 长流水 剑锋金 海中金] 星期四 北方玄武 星宿[斗木獬](吉) 彭祖百忌[癸不词讼理弱敌强 酉不会客醉坐颠狂] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁卯)兔] 煞[东] + 壹玖捌陆年肆月廿一 丙寅(虎)年 癸巳(蛇)月 癸酉(鸡)日 子(鼠)时 纳音[炉中火 长流水 剑锋金 桑柘木] 星期四 北方玄武 星宿[斗木獬](吉) 彭祖百忌[癸不词讼理弱敌强 酉不会客醉坐颠狂] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁卯)兔] 煞[东] 1986-05-29 00:00:00 星期四 双子座 ## 文档 diff --git a/README_EN.md b/README_EN.md index 011f23b..38c6453 100644 --- a/README_EN.md +++ b/README_EN.md @@ -57,7 +57,7 @@ If you will use jars in your projects, I suggest you to download latest snapshot Output: - 壹玖捌陆年肆月廿一 丙寅(虎)年 癸巳(蛇)月 癸酉(鸡)日 子(鼠)时 纳音[炉中火 长流水 剑锋金 海中金] 星期四 北方玄武 星宿[斗木獬](吉) 彭祖百忌[癸不词讼理弱敌强 酉不会客醉坐颠狂] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁卯)兔] 煞[东] + 壹玖捌陆年肆月廿一 丙寅(虎)年 癸巳(蛇)月 癸酉(鸡)日 子(鼠)时 纳音[炉中火 长流水 剑锋金 桑柘木] 星期四 北方玄武 星宿[斗木獬](吉) 彭祖百忌[癸不词讼理弱敌强 酉不会客醉坐颠狂] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁卯)兔] 煞[东] 1986-05-29 00:00:00 星期四 双子座 ## Documentation From 6f72edb85734e9be2bee22f76f64f0b186dccd43 Mon Sep 17 00:00:00 2001 From: 6tail <6tail@6tail.cn> Date: Wed, 27 May 2020 00:08:08 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A9=E6=99=9A?= =?UTF-8?q?=E5=AD=90=E6=97=B6=E5=AF=BC=E8=87=B4=E5=85=AB=E5=AD=97=E6=97=B6?= =?UTF-8?q?=E6=9F=B1=E5=92=8C=E6=97=A5=E6=9F=B1=E9=94=99=E8=AF=AF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/nlf/calendar/Lunar.java | 65 ++++++++++++++++++----- src/test/java/sample/BaZiTest.java | 8 +++ 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/nlf/calendar/Lunar.java b/src/main/java/com/nlf/calendar/Lunar.java index c860f77..75b3be9 100644 --- a/src/main/java/com/nlf/calendar/Lunar.java +++ b/src/main/java/com/nlf/calendar/Lunar.java @@ -60,6 +60,10 @@ public class Lunar{ private int dayGanIndex; /** 日对应的地支下标,0-11 */ private int dayZhiIndex; + /** 日对应的天干下标(最精确的,供八字用,晚子时算第二天),0-9 */ + private int dayGanIndexExact; + /** 日对应的地支下标(最精确的,供八字用,晚子时算第二天),0-11 */ + private int dayZhiIndexExact; /** 月对应的天干下标(以节交接当天起算),0-9 */ private int monthGanIndex; /** 月对应的地支下标(以节交接当天起算),0-11 */ @@ -326,6 +330,25 @@ public class Lunar{ int addDays = (dayOffset + LunarUtil.BASE_DAY_GANZHI_INDEX)%60; dayGanIndex = addDays%10; dayZhiIndex = addDays%12; + + int dayGanExact = dayGanIndex; + int dayZhiExact = dayZhiIndex; + + // 晚子时(夜子/子夜)应算作第二天 + String hm = (hour<10?"0":"")+hour+":"+(minute<10?"0":"")+minute; + if(hm.compareTo("23:00")>=0&&hm.compareTo("23:59")<=0){ + dayGanExact++; + if(dayGanExact>=10){ + dayGanExact -= 10; + } + dayZhiExact++; + if(dayZhiExact>=12){ + dayZhiExact -= 12; + } + } + + dayGanIndexExact = dayGanExact; + dayZhiIndexExact = dayZhiExact; } /** @@ -334,15 +357,7 @@ public class Lunar{ private void computeTime(){ String hm = (hour<10?"0":"")+hour+":"+(minute<10?"0":"")+minute; timeZhiIndex = LunarUtil.getTimeZhiIndex(hm); - int dayGan = dayGanIndex; - // 晚子时(夜子/子夜)应算作第二天 - if(hm.compareTo("23:00")>=0&&hm.compareTo("23:59")<=0){ - dayGan++; - if(dayGan>=10){ - dayGan -= 10; - } - } - timeGanIndex = (dayGan%5*2+timeZhiIndex)%10; + timeGanIndex = (dayGanIndexExact%5*2+timeZhiIndex)%10; } /** @@ -559,6 +574,15 @@ public class Lunar{ return getDayGan()+getDayZhi(); } + /** + * 获取干支纪日(日柱,晚子时算第二天) + * + * @return 干支纪日(日柱),如己卯 + */ + public String getDayInGanZhiExact(){ + return getDayGanExact()+getDayZhiExact(); + } + /** * 获取日天干 * @@ -568,6 +592,15 @@ public class Lunar{ return LunarUtil.GAN[dayGanIndex+1]; } + /** + * 获取日天干(晚子时算第二天) + * + * @return 日天干,如甲 + */ + public String getDayGanExact(){ + return LunarUtil.GAN[dayGanIndexExact+1]; + } + /** * 获取日地支 * @@ -577,6 +610,15 @@ public class Lunar{ return LunarUtil.ZHI[dayZhiIndex+1]; } + /** + * 获取日地支(晚子时算第二天) + * + * @return 日地支,如卯 + */ + public String getDayZhiExact(){ + return LunarUtil.ZHI[dayZhiIndexExact+1]; + } + /** * 获取年生肖 * @@ -1170,11 +1212,10 @@ public class Lunar{ */ public List getBaZi(){ List l = new ArrayList(4); - String timeGan = LunarUtil.GAN[(dayGanIndex%5*12+timeZhiIndex)%10+1]; l.add(getYearInGanZhiExact()); l.add(getMonthInGanZhiExact()); - l.add(getDayInGanZhi()); - l.add(timeGan+getTimeZhi()); + l.add(getDayInGanZhiExact()); + l.add(getTimeInGanZhi()); return l; } diff --git a/src/test/java/sample/BaZiTest.java b/src/test/java/sample/BaZiTest.java index 3821c46..61bb0ed 100644 --- a/src/test/java/sample/BaZiTest.java +++ b/src/test/java/sample/BaZiTest.java @@ -58,4 +58,12 @@ public class BaZiTest { System.out.println(lunar.getBaZi()); } + @Test + public void test7(){ + Solar solar = new Solar(2020,5,26,23,43,0); + Lunar lunar = solar.getLunar(); + //[庚子, 辛巳, 庚午, 丙子] + System.out.println(lunar.getBaZi()); + } + }