diff --git a/src/main/java/com/nlf/calendar/Lunar.java b/src/main/java/com/nlf/calendar/Lunar.java index 2e86e71..02600b5 100644 --- a/src/main/java/com/nlf/calendar/Lunar.java +++ b/src/main/java/com/nlf/calendar/Lunar.java @@ -9,7 +9,7 @@ import com.nlf.calendar.util.SolarUtil; /** * 农历日期 - * + * * @author 6tail * */ @@ -73,7 +73,9 @@ public class Lunar{ int diff = 0; for(int i=startY;ilastDate){ lunarD -= lastDate; lunarM = LunarUtil.nextMonth(lunarY,lunarM); - if(lunarM==1) lunarY++; + if(lunarM==1){ + lunarY++; + } lastDate = LunarUtil.getDaysOfMonth(lunarY,lunarM); } year = lunarY; @@ -116,7 +120,7 @@ public class Lunar{ /** * 获取干 - * + * * @return 干,如辛 */ public String getGan(){ @@ -125,7 +129,7 @@ public class Lunar{ /** * 获取支 - * + * * @return 支,如亥 */ public String getZhi(){ @@ -134,7 +138,7 @@ public class Lunar{ /** * 获取生肖 - * + * * @return 生肖,如虎 */ public String getShengxiao(){ @@ -143,12 +147,15 @@ public class Lunar{ /** * 获取中文的月 - * + * * @return 中文月,如正月 */ public String getMonthInChinese(){ - if(month>0) return LunarUtil.MONTH[month]; - else return "闰"+LunarUtil.MONTH[-month]; + if(month>0){ + return LunarUtil.MONTH[month]; + }else{ + return "闰"+LunarUtil.MONTH[-month]; + } } /** @@ -161,7 +168,7 @@ public class Lunar{ /** * 获取中文日 - * + * * @return 中文日,如初一 */ public String getDayInChinese(){ @@ -170,7 +177,7 @@ public class Lunar{ /** * 获取节 - * + * * @return 节 */ public String getJie(){ @@ -184,16 +191,24 @@ public class Lunar{ index++; } int term = LunarUtil.JIE_MAP[solarMonth-1][4*index+ry%4]; - if(ry==121&&solarMonth==4) term = 5; - if(ry==132&&solarMonth==4) term = 5; - if(ry==194&&solarMonth==6) term = 6; - if(solarDay==term) s = LunarUtil.JIE[solarMonth-1]; + if(ry==121&&solarMonth==4){ + term = 5; + } + if(ry==132&&solarMonth==4){ + term = 5; + } + if(ry==194&&solarMonth==6){ + term = 6; + } + if(solarDay==term){ + s = LunarUtil.JIE[solarMonth-1]; + } return s; } /** * 获取气 - * + * * @return 气 */ public String getQi(){ @@ -207,24 +222,30 @@ public class Lunar{ index++; } int term = LunarUtil.QI_MAP[solarMonth-1][4*index+ry%4]; - if(ry==171&&solarMonth==3) term = 21; - if(ry==181&&solarMonth==5) term = 21; - if(solarDay==term) s = LunarUtil.QI[solarMonth-1]; + if(ry==171&&solarMonth==3){ + term = 21; + } + if(ry==181&&solarMonth==5){ + term = 21; + } + if(solarDay==term){ + s = LunarUtil.QI[solarMonth-1]; + } return s; } /** * 获取宿 - * + * * @return 宿 */ public String getXiu(){ - return LunarUtil.XIU[day-1][month-1]; + return LunarUtil.XIU[day-1][Math.abs(month)-1]; } /** * 获取政 - * + * * @return 政 */ public String getZheng(){ @@ -257,19 +278,21 @@ public class Lunar{ /** * 获取节日,有可能一天会有多个节日 - * + * * @return 春节等 */ public List getFestivals(){ List l = new ArrayList(); String f = LunarUtil.FESTIVAL.get(month+"-"+day); - if(null!=f) l.add(f); + if(null!=f){ + l.add(f); + } return l; } /** * 转换为阳历日期 - * + * * @return 阳历日期 */ private Solar toSolar(){ @@ -281,7 +304,9 @@ public class Lunar{ while(true){ diff += LunarUtil.getDaysOfMonth(y,m); m = LunarUtil.nextMonth(y,m); - if(m==1) y++; + if(m==1){ + y++; + } if(y==year&&m==month){ diff += day; break; @@ -292,7 +317,7 @@ public class Lunar{ c.add(Calendar.DATE,diff); return new Solar(c); } - + public String toFullString(){ StringBuilder s = new StringBuilder(); s.append(toString()); @@ -321,13 +346,14 @@ public class Lunar{ return s.toString(); } + @Override public String toString(){ return getGan()+getZhi()+"年"+getMonthInChinese()+"月"+getDayInChinese(); } /** * 获取年份 - * + * * @return 如2015 */ public int getYear(){ @@ -336,7 +362,7 @@ public class Lunar{ /** * 获取月份 - * + * * @return 1到12,负数为闰月 */ public int getMonth(){ @@ -345,7 +371,7 @@ public class Lunar{ /** * 获取日期 - * + * * @return 日期 */ public int getDay(){ @@ -355,4 +381,4 @@ public class Lunar{ public Solar getSolar(){ return solar; } -} \ No newline at end of file +} diff --git a/src/main/java/com/nlf/calendar/Solar.java b/src/main/java/com/nlf/calendar/Solar.java index 0a5fd8c..c6871cd 100644 --- a/src/main/java/com/nlf/calendar/Solar.java +++ b/src/main/java/com/nlf/calendar/Solar.java @@ -8,7 +8,7 @@ import com.nlf.calendar.util.SolarUtil; /** * 阳历日期 - * + * * @author 6tail * */ @@ -31,7 +31,7 @@ public class Solar{ /** * 通过年月日初始化 - * + * * @param year 年 * @param month 月,1到12 * @param day 日,1到31 @@ -46,7 +46,7 @@ public class Solar{ /** * 通过日期初始化 - * + * * @param date 日期 */ public Solar(Date date){ @@ -56,10 +56,10 @@ public class Solar{ month = calendar.get(Calendar.MONTH)+1; day = calendar.get(Calendar.DATE); } - + /** * 通过日历初始化 - * + * * @param calendar 日历 */ public Solar(Calendar calendar){ @@ -103,7 +103,7 @@ public class Solar{ /** * 是否闰年 - * + * * @return true/false 闰年/非闰年 */ public boolean isLeapYear(){ @@ -112,7 +112,7 @@ public class Solar{ /** * 获取星期,0代表周日,1代表周一 - * + * * @return 0123456 */ public int getWeek(){ @@ -121,7 +121,7 @@ public class Solar{ /** * 获取星期的中文 - * + * * @return 日一二三四五六 */ public String getWeekInChinese(){ @@ -130,51 +130,69 @@ public class Solar{ /** * 获取节日,有可能一天会有多个节日 - * + * * @return 劳动节等 */ public List getFestivals(){ List l = new ArrayList(); //获取几月几日对应的节日 String f = SolarUtil.FESTIVAL.get(month+"-"+day); - if(null!=f) l.add(f); + if(null!=f){ + l.add(f); + } //计算几月第几个星期几对应的节日 //第几周 int weekInMonth = calendar.get(Calendar.WEEK_OF_MONTH); //星期几,0代表星期天 int week = getWeek(); //星期天很奇葩,会多算一周,需要减掉 - if(0==week) weekInMonth--; + if(0==week){ + weekInMonth--; + } f = SolarUtil.WEEK_FESTIVAL.get(month+"-"+weekInMonth+"-"+week); - if(null!=f) l.add(f); + if(null!=f){ + l.add(f); + } return l; } /** * 获取星座 - * + * * @return 星座 */ public String getXingzuo(){ int index = 11,m = month,d = day; int y = m*100+d; - if(y>=321&&y<=419) index = 0; - else if(y>=420&&y<=520) index = 1; - else if(y>=521&&y<=620) index = 2; - else if(y>=621&&y<=722) index = 3; - else if(y>=723&&y<=822) index = 4; - else if(y>=823&&y<=922) index = 5; - else if(y>=923&&y<=1022) index = 6; - else if(y>=1023&&y<=1121) index = 7; - else if(y>=1122&&y<=1221) index = 8; - else if(y>=1222||y<=119) index = 9; - else if(y<=218) index = 10; + if(y>=321&&y<=419){ + index = 0; + }else if(y>=420&&y<=520){ + index = 1; + }else if(y>=521&&y<=620){ + index = 2; + }else if(y>=621&&y<=722){ + index = 3; + }else if(y>=723&&y<=822){ + index = 4; + }else if(y>=823&&y<=922){ + index = 5; + }else if(y>=923&&y<=1022){ + index = 6; + }else if(y>=1023&&y<=1121){ + index = 7; + }else if(y>=1122&&y<=1221){ + index = 8; + }else if(y>=1222||y<=119){ + index = 9; + }else if(y<=218){ + index = 10; + } return SolarUtil.XINGZUO[index]; } /** * 获取年份 - * + * * @return 如2015 */ public int getYear(){ @@ -183,7 +201,7 @@ public class Solar{ /** * 获取月份 - * + * * @return 1到12 */ public int getMonth(){ @@ -192,13 +210,13 @@ public class Solar{ /** * 获取日期 - * + * * @return 1到31之间的数字 */ public int getDay(){ return day; } - + /** * 获取农历 * @return 农历 @@ -209,17 +227,18 @@ public class Solar{ /** * 获取日历 - * + * * @return 日历 */ public Calendar getCalendar(){ return calendar; } + @Override public String toString(){ return year+"-"+(month<10?"0":"")+month+"-"+(day<10?"0":"")+day; } - + public String toFullString(){ StringBuilder s = new StringBuilder(); s.append(toString()); @@ -253,4 +272,4 @@ public class Solar{ return new Solar(c); } -} \ No newline at end of file +} diff --git a/src/main/java/com/nlf/calendar/SolarHalfYear.java b/src/main/java/com/nlf/calendar/SolarHalfYear.java index 43b3259..6b095ff 100644 --- a/src/main/java/com/nlf/calendar/SolarHalfYear.java +++ b/src/main/java/com/nlf/calendar/SolarHalfYear.java @@ -7,6 +7,8 @@ import java.util.List; /** * 阳历半年 + * + * @author 6tail */ public class SolarHalfYear { /** 年 */ @@ -14,6 +16,9 @@ public class SolarHalfYear { /** 月 */ private int month; + /** 半年的月数 */ + public static final int MONTH_COUNT = 6; + /** * 默认当月 */ @@ -104,7 +109,7 @@ public class SolarHalfYear { * @return 半年序号,从1开始 */ public int getIndex(){ - return (int)Math.ceil(month/6D); + return (int)Math.ceil(month*1D/MONTH_COUNT); } /** @@ -118,7 +123,7 @@ public class SolarHalfYear { } Calendar c = Calendar.getInstance(); c.set(year,month-1,1); - c.add(Calendar.MONTH,6*halfYears); + c.add(Calendar.MONTH,MONTH_COUNT*halfYears); return new SolarHalfYear(c); } @@ -129,12 +134,13 @@ public class SolarHalfYear { public List getMonths(){ List l = new ArrayList(); int index = getIndex()-1; - for(int i=0;i<6;i++){ - l.add(new SolarMonth(year,6*index+i+1)); + for(int i=0;i getDays(){ @@ -118,7 +118,7 @@ public class SolarMonth{ } return l; } - + /** * 获取往后推几个月的阳历月,如果要往前推,则月数用负数 * @param months 月数 @@ -131,6 +131,7 @@ public class SolarMonth{ return new SolarMonth(c); } + @Override public String toString(){ return year+"-"+month; } @@ -138,4 +139,4 @@ public class SolarMonth{ public String toFullString(){ return year+"年"+month+"月"; } -} \ No newline at end of file +} diff --git a/src/main/java/com/nlf/calendar/SolarSeason.java b/src/main/java/com/nlf/calendar/SolarSeason.java index fe18d83..578d2f8 100644 --- a/src/main/java/com/nlf/calendar/SolarSeason.java +++ b/src/main/java/com/nlf/calendar/SolarSeason.java @@ -7,12 +7,16 @@ import java.util.List; /** * 阳历季度 + * + * @author 6tail */ public class SolarSeason { /** 年 */ private int year; /** 月 */ private int month; + /** 一个季度的月数 */ + public static final int MONTH_COUNT = 3; /** * 默认当月 @@ -104,7 +108,7 @@ public class SolarSeason { * @return 季度序号,从1开始 */ public int getIndex(){ - return (int)Math.ceil(month/3D); + return (int)Math.ceil(month*1D/MONTH_COUNT); } /** @@ -118,7 +122,7 @@ public class SolarSeason { } Calendar c = Calendar.getInstance(); c.set(year,month-1,1); - c.add(Calendar.MONTH,3*seasons); + c.add(Calendar.MONTH,MONTH_COUNT*seasons); return new SolarSeason(c); } @@ -129,12 +133,13 @@ public class SolarSeason { public List getMonths(){ List l = new ArrayList(); int index = getIndex()-1; - for(int i=0;i<3;i++){ - l.add(new SolarMonth(year,3*index+i+1)); + for(int i=0;i getMonths(){ - List l = new ArrayList(12); + List l = new ArrayList(MONTH_COUNT); SolarMonth m = new SolarMonth(year,1); l.add(m); - for(int i = 1;i<12;i++){ + for(int i = 1;i