diff --git a/README.md b/README.md index 45d1640..67eb1df 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # lunar [![License](https://img.shields.io/badge/license-MIT-4EB1BA.svg?style=flat-square)](https://github.com/6tail/lunar/blob/master/LICENSE) -lunar is a calendar library for Solar and Chinese Lunar. +lunar是一个无依赖的支持阳历和阴历的日历工具库。 -> Support since java 1.5 +> 支持java 1.5及以上版本。 -[简体中文](https://github.com/6tail/lunar-java/blob/master/README_ZH.md) +[English](https://github.com/6tail/lunar-java/blob/master/README_EN.md) -### Release +### 稳定版本 ```xml @@ -16,7 +16,7 @@ lunar is a calendar library for Solar and Chinese Lunar. ``` -### Snapshot +### 快照版本 ```xml @@ -38,13 +38,13 @@ lunar is a calendar library for Solar and Chinese Lunar. ``` -### Download +### 下载jar -If you will use jars in your projects, I suggest you to download latest snapshot versions with less bugs. +如果使用jar,建议下载最新的SNAPSHOT版本,bug将得到及时的修复。 [Download](https://oss.sonatype.org/content/groups/public/cn/6tail/lunar/) -## Example +## 示例 import com.nlf.calendar.Lunar; @@ -68,20 +68,19 @@ If you will use jars in your projects, I suggest you to download latest snapshot } } -Output: +输出结果: - - 丙申年捌月初八 猴年丁酉月癸巳日 北方玄武 斗木獬 彭祖[癸不词讼理弱敌强 巳不远行财物伏藏] + 贰零壹陆年捌月初八 丙申(猴)年丁酉月癸巳日 北方玄武 斗木獬 彭祖百忌[癸不词讼理弱敌强 巳不远行财物伏藏] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁亥)猪] 刹[东] 2016-09-08 闰年 星期四 (世界扫盲日) 处女座 - 丙寅年肆月廿一 虎年癸巳月癸酉日 北方玄武 危月燕 彭祖[癸不词讼理弱敌强 酉不会客醉坐颠狂] + 壹玖捌陆年肆月廿一 丙寅(虎)年癸巳月癸酉日 北方玄武 危月燕 彭祖百忌[癸不词讼理弱敌强 酉不会客醉坐颠狂] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁卯)兔] 刹[东] 1986-05-29 星期四 双子座 -## Documentation +## 文档 -Please visit [http://6tail.cn/calendar/api.html](http://6tail.cn/calendar/api.html "http://6tail.cn/calendar/api.html") +请移步至 [http://6tail.cn/calendar/api.html](http://6tail.cn/calendar/api.html "http://6tail.cn/calendar/api.html") -## Contact +## 联系 lunar diff --git a/README_ZH.md b/README_EN.md similarity index 61% rename from README_ZH.md rename to README_EN.md index ab6afae..d072d98 100644 --- a/README_ZH.md +++ b/README_EN.md @@ -1,12 +1,12 @@ # lunar [![License](https://img.shields.io/badge/license-MIT-4EB1BA.svg?style=flat-square)](https://github.com/6tail/lunar/blob/master/LICENSE) -lunar是一个无依赖的支持阳历和阴历的日历工具库。 +lunar is a calendar library for Solar and Chinese Lunar. -> 支持java 1.5及以上版本。 +> Support since java 1.5 -[English](https://github.com/6tail/lunar-java/blob/master/README.md) +[简体中文](https://github.com/6tail/lunar-java/blob/master/README_ZH.md) -### 稳定版本 +### Release ```xml @@ -16,7 +16,7 @@ lunar是一个无依赖的支持阳历和阴历的日历工具库。 ``` -### 快照版本 +### Snapshot ```xml @@ -38,13 +38,13 @@ lunar是一个无依赖的支持阳历和阴历的日历工具库。 ``` -### 下载jar +### Download -如果使用jar,建议下载最新的SNAPSHOT版本,bug将得到及时的修复。 +If you will use jars in your projects, I suggest you to download latest snapshot versions with less bugs. [Download](https://oss.sonatype.org/content/groups/public/cn/6tail/lunar/) -## 示例 +## Example import com.nlf.calendar.Lunar; @@ -68,20 +68,19 @@ lunar是一个无依赖的支持阳历和阴历的日历工具库。 } } -输出结果: +Output: - - 丙申年捌月初八 猴年丁酉月癸巳日 北方玄武 斗木獬 彭祖[癸不词讼理弱敌强 巳不远行财物伏藏] + 贰零壹陆年捌月初八 丙申(猴)年丁酉月癸巳日 北方玄武 斗木獬 彭祖百忌[癸不词讼理弱敌强 巳不远行财物伏藏] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁亥)猪] 刹[东] 2016-09-08 闰年 星期四 (世界扫盲日) 处女座 - 丙寅年肆月廿一 虎年癸巳月癸酉日 北方玄武 危月燕 彭祖[癸不词讼理弱敌强 酉不会客醉坐颠狂] + 壹玖捌陆年肆月廿一 丙寅(虎)年癸巳月癸酉日 北方玄武 危月燕 彭祖百忌[癸不词讼理弱敌强 酉不会客醉坐颠狂] 喜神方位[巽](东南) 阳贵神方位[巽](东南) 阴贵神方位[震](正东) 福神方位[兑](正西) 财神方位[离](正南) 冲[(丁卯)兔] 刹[东] 1986-05-29 星期四 双子座 -## 文档 +## Documentation -请移步至 [http://6tail.cn/calendar/api.html](http://6tail.cn/calendar/api.html "http://6tail.cn/calendar/api.html") +Please visit [http://6tail.cn/calendar/api.html](http://6tail.cn/calendar/api.html "http://6tail.cn/calendar/api.html") -## 联系 +## Contact lunar diff --git a/src/main/java/com/nlf/calendar/Lunar.java b/src/main/java/com/nlf/calendar/Lunar.java index 5363156..58b5945 100644 --- a/src/main/java/com/nlf/calendar/Lunar.java +++ b/src/main/java/com/nlf/calendar/Lunar.java @@ -22,6 +22,12 @@ public class Lunar{ private int day; /** 对应阳历 */ private Solar solar; + /** 相对于基准日的偏移天数 */ + private int dayOffset; + /** 日对应的天干下标,0-10 */ + private int dayGanIndex; + /** 日对应的地支下标,0-12 */ + private int dayZhiIndex; /** * 默认使用当前日期初始化 @@ -41,65 +47,73 @@ public class Lunar{ this.year = year; this.month = month; this.day = day; + this.dayOffset = LunarUtil.computeAddDays(year,month,day); + int addDays = (dayOffset + LunarUtil.BASE_DAY_GANZHI_INDEX)%60; + dayGanIndex = addDays%10; + dayZhiIndex = addDays%12; this.solar = toSolar(); } /** - * 通过日期初始化 - * @param date 日期 + * 通过阳历日期初始化 + * @param date 阳历日期 */ public Lunar(Date date){ solar = new Solar(date); int y = solar.getYear(); int m = solar.getMonth(); int d = solar.getDay(); - int startY,startM,startD; - int lunarY,lunarM,lunarD; + int startYear,startMonth,startDay; + int lunarYear,lunarMonth,lunarDay; if(y<2000){ - startY = SolarUtil.BASE_YEAR; - startM = SolarUtil.BASE_MONTH; - startD = SolarUtil.BASE_DAY; - lunarY = LunarUtil.BASE_YEAR; - lunarM = LunarUtil.BASE_MONTH; - lunarD = LunarUtil.BASE_DAY; + startYear = SolarUtil.BASE_YEAR; + startMonth = SolarUtil.BASE_MONTH; + startDay = SolarUtil.BASE_DAY; + lunarYear = LunarUtil.BASE_YEAR; + lunarMonth = LunarUtil.BASE_MONTH; + lunarDay = LunarUtil.BASE_DAY; }else{ - startY = SolarUtil.BASE_YEAR+99; - startM = 1; - startD = 1; - lunarY = LunarUtil.BASE_YEAR+99; - lunarM = 11; - lunarD = 25; + startYear = SolarUtil.BASE_YEAR+99; + startMonth = 1; + startDay = 1; + lunarYear = LunarUtil.BASE_YEAR+99; + lunarMonth = 11; + lunarDay = 25; } int diff = 0; - for(int i=startY;ilastDate){ - lunarD -= lastDate; - lunarM = LunarUtil.nextMonth(lunarY,lunarM); - if(lunarM==1){ - lunarY++; + diff += d-startDay; + lunarDay += diff; + int lastDate = LunarUtil.getDaysOfMonth(lunarYear,lunarMonth); + while(lunarDay>lastDate){ + lunarDay -= lastDate; + lunarMonth = LunarUtil.nextMonth(lunarYear,lunarMonth); + if(lunarMonth==1){ + lunarYear++; } - lastDate = LunarUtil.getDaysOfMonth(lunarY,lunarM); + lastDate = LunarUtil.getDaysOfMonth(lunarYear,lunarMonth); } - year = lunarY; - month = lunarM; - day = lunarD; + year = lunarYear; + month = lunarMonth; + day = lunarDay; + dayOffset = LunarUtil.computeAddDays(year,month,day); + int addDays = (dayOffset + LunarUtil.BASE_DAY_GANZHI_INDEX)%60; + dayGanIndex = addDays%10; + dayZhiIndex = addDays%12; } /** - * 通过指定日期获取农历 + * 通过指定阳历日期获取农历 * - * @param date 日期 + * @param date 阳历日期 * @return 农历 */ public static Lunar fromDate(Date date){ @@ -145,6 +159,20 @@ public class Lunar{ return LunarUtil.SHENGXIAO[(year-4)%12+1]; } + /** + * 获取中文的年 + * + * @return 中文年,如二零零一 + */ + public String getYearInChinese(){ + String y = (year+""); + StringBuilder s = new StringBuilder(); + for(int i=0,j=y.length();i CHONG = new HashMap(){ + private static final long serialVersionUID = -1; + { + put("子","午"); + put("丑","未"); + put("寅","申"); + put("卯","酉"); + put("辰","戌"); + put("巳","亥"); + put("午","子"); + put("未","丑"); + put("申","寅"); + put("酉","卯"); + put("戌","辰"); + put("亥","巳"); + } + }; + /** 天干相冲之无情之克(阳克阳,阴克阴) */ + public static final Map CHONG_GAN = new HashMap(){ + private static final long serialVersionUID = -1; + { + put("甲","戊"); + put("乙","己"); + put("丙","庚"); + put("丁","辛"); + put("戊","壬"); + put("己","癸"); + put("庚","甲"); + put("辛","乙"); + put("壬","丙"); + put("癸","丁"); + } + }; + /** 天干四冲(无情之克中克得最严重的4个) */ + public static final Map CHONG_GAN_BAD = new HashMap(){ + private static final long serialVersionUID = -1; + { + put("庚","甲"); + put("辛","乙"); + put("壬","丙"); + put("癸","丁"); + } + }; + /** 天干相冲之有情之克(阳克阴,阴克阳) */ + public static final Map CHONG_GAN_TIE = new HashMap(){ + private static final long serialVersionUID = -1; + { + put("甲","己"); + put("乙","戊"); + put("丙","辛"); + put("丁","庚"); + put("戊","癸"); + put("己","壬"); + put("庚","乙"); + put("辛","甲"); + put("壬","丁"); + put("癸","丙"); + } + }; + /** 天干五合(有情之克中最有情的5个) */ + public static final Map CHONG_GAN_TIE_GOOD = new HashMap(){ + private static final long serialVersionUID = -1; + { + put("甲","己"); + put("丙","辛"); + put("戊","癸"); + put("庚","乙"); + put("壬","丁"); + } + }; + /** 刹(逢巳日、酉日、丑日必煞东;亥日、卯日、未日必煞西;申日、子日、辰日必煞南;寅日、午日、戌日必煞北) */ + public static final Map SHA = new HashMap(){ + private static final long serialVersionUID = -1; + { + put("子","南"); + put("丑","东"); + put("寅","北"); + put("卯","西"); + put("辰","南"); + put("巳","东"); + put("午","北"); + put("未","西"); + put("申","南"); + put("酉","东"); + put("戌","北"); + put("亥","西"); + } + }; + /** 方位 */ + public static final Map POSITION_DESC = new HashMap(){ + private static final long serialVersionUID = -1; + { + put("坎","正北"); + put("艮","东北"); + put("震","正东"); + put("巽","东南"); + put("离","正南"); + put("坤","西南"); + put("兑","正西"); + put("乾","西北"); + } + }; /** 宫 */ public static final Map GONG = new HashMap(){ private static final long serialVersionUID = -1; diff --git a/src/test/java/test/LunarTest.java b/src/test/java/test/LunarTest.java index 1962133..9d3f43b 100644 --- a/src/test/java/test/LunarTest.java +++ b/src/test/java/test/LunarTest.java @@ -14,8 +14,8 @@ public class LunarTest { @Test public void test(){ Lunar date = new Lunar(2019,3,27); - Assert.assertEquals("己亥年叁月廿七",date.toString()); - Assert.assertEquals("己亥年叁月廿七 猪年戊辰月戊戌日 (七殿泰山王诞) 西方白虎 娄金狗 彭祖[戊不受田田主不祥 戌不吃犬作怪上床]",date.toFullString()); + Assert.assertEquals("贰零壹玖年叁月廿七",date.toString()); + Assert.assertEquals("贰零壹玖年叁月廿七 己亥(猪)年戊辰月戊戌日 (七殿泰山王诞) 西方白虎 娄金狗 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬辰)龙] 刹[北]",date.toFullString()); Assert.assertEquals("2019-05-01",date.getSolar().toString()); Assert.assertEquals("2019-05-01 星期三 (劳动节) 金牛座",date.getSolar().toFullString()); } diff --git a/src/test/java/test/SolarTest.java b/src/test/java/test/SolarTest.java index a38ae57..bd5d7bd 100644 --- a/src/test/java/test/SolarTest.java +++ b/src/test/java/test/SolarTest.java @@ -16,8 +16,8 @@ public class SolarTest { Solar date = new Solar(2019,5,1); Assert.assertEquals("2019-05-01",date.toString()); Assert.assertEquals("2019-05-01 星期三 (劳动节) 金牛座",date.toFullString()); - Assert.assertEquals("己亥年叁月廿七",date.getLunar().toString()); - Assert.assertEquals("己亥年叁月廿七 猪年戊辰月戊戌日 (七殿泰山王诞) 西方白虎 娄金狗 彭祖[戊不受田田主不祥 戌不吃犬作怪上床]",date.getLunar().toFullString()); + Assert.assertEquals("贰零壹玖年叁月廿七",date.getLunar().toString()); + Assert.assertEquals("贰零壹玖年叁月廿七 己亥(猪)年戊辰月戊戌日 (七殿泰山王诞) 西方白虎 娄金狗 彭祖百忌[戊不受田田主不祥 戌不吃犬作怪上床] 喜神方位[巽](东南) 阳贵神方位[艮](东北) 阴贵神方位[坤](西南) 福神方位[坎](正北) 财神方位[坎](正北) 冲[(壬辰)龙] 刹[北]",date.getLunar().toFullString()); } }