diff --git a/src/main/java/com/nlf/calendar/ExactDate.java b/src/main/java/com/nlf/calendar/ExactDate.java new file mode 100644 index 0000000..2ee09de --- /dev/null +++ b/src/main/java/com/nlf/calendar/ExactDate.java @@ -0,0 +1,30 @@ +package com.nlf.calendar; + +import java.util.Calendar; +import java.util.Date; + +/** + * 精确日期 + * + * @author 6tail + */ +public class ExactDate { + + public static Calendar fromYmdHms(int year, int month, int day, int hour, int minute, int second) { + Calendar c = Calendar.getInstance(); + c.set(year, month - 1, day, hour, minute, second); + c.set(Calendar.MILLISECOND, 0); + return c; + } + + public static Calendar fromYmd(int year, int month, int day) { + return fromYmdHms(year, month, day, 0, 0, 0); + } + + public static Calendar fromDate(Date date) { + Calendar c = Calendar.getInstance(); + c.setTime(date); + c.set(Calendar.MILLISECOND, 0); + return c; + } +} diff --git a/src/main/java/com/nlf/calendar/Lunar.java b/src/main/java/com/nlf/calendar/Lunar.java index 9fff4fd..6f60dfa 100644 --- a/src/main/java/com/nlf/calendar/Lunar.java +++ b/src/main/java/com/nlf/calendar/Lunar.java @@ -1,10 +1,10 @@ package com.nlf.calendar; -import java.util.*; - import com.nlf.calendar.util.LunarUtil; import com.nlf.calendar.util.SolarUtil; +import java.util.*; + /** * 农历日期 * @@ -227,9 +227,7 @@ public class Lunar { @SuppressWarnings("MagicConstant") public Lunar(Date date) { solar = new Solar(date); - Calendar c = Calendar.getInstance(); - c.set(solar.getYear(), solar.getMonth() - 1, solar.getDay(), 0, 0, 0); - c.set(Calendar.MILLISECOND, 0); + Calendar c = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay()); long solarTime = c.getTimeInMillis(); int y = solar.getYear(); LunarYear ly = LunarYear.fromYear(y); @@ -2529,23 +2527,17 @@ public class Lunar { */ @SuppressWarnings("MagicConstant") public ShuJiu getShuJiu() { - Calendar currentCalendar = Calendar.getInstance(); - currentCalendar.set(solar.getYear(), solar.getMonth() - 1, solar.getDay(), 0, 0, 0); - currentCalendar.set(Calendar.MILLISECOND, 0); + Calendar currentCalendar = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay()); Solar start = jieQi.get(JIE_QI_APPEND); - Calendar startCalendar = Calendar.getInstance(); - startCalendar.set(start.getYear(), start.getMonth() - 1, start.getDay(), 0, 0, 0); - startCalendar.set(Calendar.MILLISECOND, 0); + Calendar startCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay()); if (currentCalendar.compareTo(startCalendar) < 0) { start = jieQi.get(JIE_QI_FIRST); - startCalendar.set(start.getYear(), start.getMonth() - 1, start.getDay(), 0, 0, 0); + startCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay()); } - Calendar endCalendar = Calendar.getInstance(); - endCalendar.set(start.getYear(), start.getMonth() - 1, start.getDay(), 0, 0, 0); + Calendar endCalendar = ExactDate.fromYmd(start.getYear(), start.getMonth(), start.getDay()); endCalendar.add(Calendar.DATE, 81); - endCalendar.set(Calendar.MILLISECOND, 0); if (currentCalendar.compareTo(startCalendar) < 0 || currentCalendar.compareTo(endCalendar) >= 0) { return null; @@ -2562,14 +2554,10 @@ public class Lunar { */ @SuppressWarnings("MagicConstant") public Fu getFu() { - Calendar currentCalendar = Calendar.getInstance(); - currentCalendar.set(solar.getYear(), solar.getMonth() - 1, solar.getDay(), 0, 0, 0); - currentCalendar.set(Calendar.MILLISECOND, 0); + Calendar currentCalendar = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay()); Solar xiaZhi = jieQi.get("夏至"); Solar liQiu = jieQi.get("立秋"); - Calendar startCalendar = Calendar.getInstance(); - startCalendar.set(xiaZhi.getYear(), xiaZhi.getMonth() - 1, xiaZhi.getDay(), 0, 0, 0); - startCalendar.set(Calendar.MILLISECOND, 0); + Calendar startCalendar = ExactDate.fromYmd(xiaZhi.getYear(), xiaZhi.getMonth(), xiaZhi.getDay()); // 第1个庚日 int add = 6 - xiaZhi.getLunar().getDayGanIndex(); if (add < 0) { @@ -2600,9 +2588,7 @@ public class Lunar { // 第5个庚日,中伏第11天或末伏第1天 startCalendar.add(Calendar.DATE, 10); - Calendar liQiuCalendar = Calendar.getInstance(); - liQiuCalendar.set(liQiu.getYear(), liQiu.getMonth() - 1, liQiu.getDay(), 0, 0, 0); - liQiuCalendar.set(Calendar.MILLISECOND, 0); + Calendar liQiuCalendar = ExactDate.fromYmd(liQiu.getYear(), liQiu.getMonth(), liQiu.getDay()); days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY); // 末伏 @@ -2650,14 +2636,10 @@ public class Lunar { break; } } - Calendar currentCalendar = Calendar.getInstance(); - currentCalendar.set(solar.getYear(), solar.getMonth() - 1, solar.getDay(), 0, 0, 0); - currentCalendar.set(Calendar.MILLISECOND, 0); + Calendar currentCalendar = ExactDate.fromYmd(solar.getYear(), solar.getMonth(), solar.getDay()); Solar startSolar = jieQi.getSolar(); - Calendar startCalendar = Calendar.getInstance(); - startCalendar.set(startSolar.getYear(), startSolar.getMonth() - 1, startSolar.getDay(), 0, 0, 0); - startCalendar.set(Calendar.MILLISECOND, 0); + Calendar startCalendar = ExactDate.fromYmd(startSolar.getYear(), startSolar.getMonth(), startSolar.getDay()); int days = (int) ((currentCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / MS_PER_DAY); return LunarUtil.WU_HOU[offset * 3 + days / 5]; diff --git a/src/main/java/com/nlf/calendar/Solar.java b/src/main/java/com/nlf/calendar/Solar.java index 6eef229..d123514 100644 --- a/src/main/java/com/nlf/calendar/Solar.java +++ b/src/main/java/com/nlf/calendar/Solar.java @@ -13,59 +13,72 @@ import com.nlf.calendar.util.SolarUtil; * 阳历日期 * * @author 6tail - * */ -public class Solar{ - /** 2000年儒略日数(2000-1-1 12:00:00 UTC) */ +public class Solar { + /** + * 2000年儒略日数(2000-1-1 12:00:00 UTC) + */ public static final double J2000 = 2451545; - /** 年 */ + /** + * 年 + */ private int year; - /** 月 */ + /** + * 月 + */ private int month; - /** 日 */ + /** + * 日 + */ private int day; - /** 时 */ + /** + * 时 + */ private int hour; - /** 分 */ + /** + * 分 + */ private int minute; - /** 秒 */ + /** + * 秒 + */ private int second; - /** 日历 */ + /** + * 日历 + */ private Calendar calendar; /** * 默认使用当前日期初始化 */ - public Solar(){ + public Solar() { this(new Date()); } /** * 通过年月日初始化 * - * @param year 年 + * @param year 年 * @param month 月,1到12 - * @param day 日,1到31 + * @param day 日,1到31 */ - public Solar(int year,int month,int day){ - this(year,month,day,0,0,0); + public Solar(int year, int month, int day) { + this(year, month, day, 0, 0, 0); } /** * 通过年月日初始化 * - * @param year 年 - * @param month 月,1到12 - * @param day 日,1到31 - * @param hour 小时,0到23 + * @param year 年 + * @param month 月,1到12 + * @param day 日,1到31 + * @param hour 小时,0到23 * @param minute 分钟,0到59 * @param second 秒钟,0到59 */ @SuppressWarnings("MagicConstant") - public Solar(int year, int month, int day, int hour, int minute, int second){ - calendar = Calendar.getInstance(); - calendar.set(year,month-1,day,hour,minute,second); - calendar.set(Calendar.MILLISECOND,0); + public Solar(int year, int month, int day, int hour, int minute, int second) { + calendar = ExactDate.fromYmdHms(year, month, day, hour, minute, second); this.year = year; this.month = month; this.day = day; @@ -79,12 +92,10 @@ public class Solar{ * * @param date 日期 */ - public Solar(Date date){ - calendar = Calendar.getInstance(); - calendar.setTime(date); - calendar.set(Calendar.MILLISECOND,0); + public Solar(Date date) { + calendar = ExactDate.fromDate(date); year = calendar.get(Calendar.YEAR); - month = calendar.get(Calendar.MONTH)+1; + month = calendar.get(Calendar.MONTH) + 1; day = calendar.get(Calendar.DATE); hour = calendar.get(Calendar.HOUR_OF_DAY); minute = calendar.get(Calendar.MINUTE); @@ -96,11 +107,11 @@ public class Solar{ * * @param calendar 日历 */ - public Solar(Calendar calendar){ - calendar.set(Calendar.MILLISECOND,0); + public Solar(Calendar calendar) { + calendar.set(Calendar.MILLISECOND, 0); this.calendar = calendar; year = calendar.get(Calendar.YEAR); - month = calendar.get(Calendar.MONTH)+1; + month = calendar.get(Calendar.MONTH) + 1; day = calendar.get(Calendar.DATE); hour = calendar.get(Calendar.HOUR_OF_DAY); minute = calendar.get(Calendar.MINUTE); @@ -109,23 +120,24 @@ public class Solar{ /** * 通过儒略日初始化 + * * @param julianDay 儒略日 */ @SuppressWarnings("MagicConstant") - public Solar(double julianDay){ - int d = (int)(julianDay + 0.5); + public Solar(double julianDay) { + int d = (int) (julianDay + 0.5); double f = julianDay + 0.5 - d; int c; if (d >= 2299161) { - c = (int)((d - 1867216.25) / 36524.25); - d += 1 + c - (int)(c * 1D / 4); + c = (int) ((d - 1867216.25) / 36524.25); + d += 1 + c - (int) (c * 1D / 4); } d += 1524; - int year = (int)((d - 122.1) / 365.25); - d -= (int)(365.25 * year); - int month = (int)(d * 1D / 30.601); - d -= (int)(30.601 * month); + int year = (int) ((d - 122.1) / 365.25); + d -= (int) (365.25 * year); + int month = (int) (d * 1D / 30.601); + d -= (int) (30.601 * month); int day = d; if (month > 13) { month -= 13; @@ -135,19 +147,17 @@ public class Solar{ year -= 4716; } f *= 24; - int hour = (int)f; + int hour = (int) f; f -= hour; f *= 60; - int minute = (int)f; + int minute = (int) f; f -= minute; f *= 60; - int second = (int)Math.round(f); + int second = (int) Math.round(f); - calendar = Calendar.getInstance(); - calendar.set(year,month-1,day,hour,minute,second); - calendar.set(Calendar.MILLISECOND,0); + calendar = ExactDate.fromYmdHms(year, month, day, hour, minute, second); this.year = year; this.month = month; this.day = day; @@ -162,7 +172,7 @@ public class Solar{ * @param date 日期 * @return 阳历 */ - public static Solar fromDate(Date date){ + public static Solar fromDate(Date date) { return new Solar(date); } @@ -172,7 +182,7 @@ public class Solar{ * @param calendar 日历 * @return 阳历 */ - public static Solar fromCalendar(Calendar calendar){ + public static Solar fromCalendar(Calendar calendar) { return new Solar(calendar); } @@ -182,116 +192,119 @@ public class Solar{ * @param julianDay 儒略日 * @return 阳历 */ - public static Solar fromJulianDay(double julianDay){ + public static Solar fromJulianDay(double julianDay) { return new Solar(julianDay); } /** * 通过指定年月日获取阳历 * - * @param year 年 + * @param year 年 * @param month 月,1到12 - * @param day 日,1到31 + * @param day 日,1到31 * @return 阳历 */ - public static Solar fromYmd(int year,int month,int day){ - return new Solar(year,month,day); + public static Solar fromYmd(int year, int month, int day) { + return new Solar(year, month, day); } /** * 通过指定年月日时分获取阳历 * - * @param year 年 - * @param month 月,1到12 - * @param day 日,1到31 - * @param hour 小时,0到23 + * @param year 年 + * @param month 月,1到12 + * @param day 日,1到31 + * @param hour 小时,0到23 * @param minute 分钟,0到59 * @param second 秒钟,0到59 * @return 阳历 */ - public static Solar fromYmdHms(int year,int month,int day,int hour,int minute,int second){ - return new Solar(year,month,day,hour,minute,second); + public static Solar fromYmdHms(int year, int month, int day, int hour, int minute, int second) { + return new Solar(year, month, day, hour, minute, second); } /** * 通过八字获取阳历列表(晚子时日柱按当天,起始年为1900) - * @param yearGanZhi 年柱 + * + * @param yearGanZhi 年柱 * @param monthGanZhi 月柱 - * @param dayGanZhi 日柱 - * @param timeGanZhi 时柱 + * @param dayGanZhi 日柱 + * @param timeGanZhi 时柱 * @return 符合的阳历列表 */ - public static List fromBaZi(String yearGanZhi,String monthGanZhi,String dayGanZhi,String timeGanZhi){ - return fromBaZi(yearGanZhi,monthGanZhi,dayGanZhi,timeGanZhi,2); + public static List fromBaZi(String yearGanZhi, String monthGanZhi, String dayGanZhi, String timeGanZhi) { + return fromBaZi(yearGanZhi, monthGanZhi, dayGanZhi, timeGanZhi, 2); } /** * 通过八字获取阳历列表(起始年为1900) - * @param yearGanZhi 年柱 + * + * @param yearGanZhi 年柱 * @param monthGanZhi 月柱 - * @param dayGanZhi 日柱 - * @param timeGanZhi 时柱 - * @param sect 流派,2晚子时日柱按当天,1晚子时日柱按明天 + * @param dayGanZhi 日柱 + * @param timeGanZhi 时柱 + * @param sect 流派,2晚子时日柱按当天,1晚子时日柱按明天 * @return 符合的阳历列表 */ - public static List fromBaZi(String yearGanZhi,String monthGanZhi,String dayGanZhi,String timeGanZhi,int sect){ - return fromBaZi(yearGanZhi,monthGanZhi,dayGanZhi,timeGanZhi,sect,1900); + public static List fromBaZi(String yearGanZhi, String monthGanZhi, String dayGanZhi, String timeGanZhi, int sect) { + return fromBaZi(yearGanZhi, monthGanZhi, dayGanZhi, timeGanZhi, sect, 1900); } /** * 通过八字获取阳历列表 - * @param yearGanZhi 年柱 + * + * @param yearGanZhi 年柱 * @param monthGanZhi 月柱 - * @param dayGanZhi 日柱 - * @param timeGanZhi 时柱 - * @param sect 流派,2晚子时日柱按当天,1晚子时日柱按明天 - * @param baseYear 起始年 + * @param dayGanZhi 日柱 + * @param timeGanZhi 时柱 + * @param sect 流派,2晚子时日柱按当天,1晚子时日柱按明天 + * @param baseYear 起始年 * @return 符合的阳历列表 */ - public static List fromBaZi(String yearGanZhi,String monthGanZhi,String dayGanZhi,String timeGanZhi,int sect,int baseYear){ - sect = (1==sect)?1:2; + public static List fromBaZi(String yearGanZhi, String monthGanZhi, String dayGanZhi, String timeGanZhi, int sect, int baseYear) { + sect = (1 == sect) ? 1 : 2; List l = new ArrayList(); Solar today = new Solar(); Lunar lunar = today.getLunar(); - int offsetYear = LunarUtil.getJiaZiIndex(lunar.getYearInGanZhiExact())-LunarUtil.getJiaZiIndex(yearGanZhi); - if(offsetYear<0){ - offsetYear = offsetYear+60; + int offsetYear = LunarUtil.getJiaZiIndex(lunar.getYearInGanZhiExact()) - LunarUtil.getJiaZiIndex(yearGanZhi); + if (offsetYear < 0) { + offsetYear = offsetYear + 60; } int startYear = today.getYear() - offsetYear; int hour = 0; String timeZhi = timeGanZhi.substring(1); - for(int i=0,j=LunarUtil.ZHI.length;i=baseYear){ - int year = startYear-1; + while (startYear >= baseYear) { + int year = startYear - 1; int counter = 0; int month = 12; int day; boolean found = false; while (counter < 15) { - if(year>=baseYear){ + if (year >= baseYear) { day = 1; Solar solar = new Solar(year, month, day, hour, 0, 0); lunar = solar.getLunar(); - if(lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi)){ + if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi)) { found = true; break; } } month++; - if(month > 12){ + if (month > 12) { month = 1; year++; } counter++; } - if(found){ + if (found) { counter = 0; month--; - if(month<1){ + if (month < 1) { month = 12; year--; } @@ -299,7 +312,7 @@ public class Solar{ Solar solar = new Solar(year, month, day, hour, 0, 0); while (counter < 61) { lunar = solar.getLunar(); - String dgz = (2==sect)?lunar.getDayInGanZhiExact2():lunar.getDayInGanZhiExact(); + String dgz = (2 == sect) ? lunar.getDayInGanZhiExact2() : lunar.getDayInGanZhiExact(); if (lunar.getYearInGanZhiExact().equals(yearGanZhi) && lunar.getMonthInGanZhiExact().equals(monthGanZhi) && dgz.equals(dayGanZhi) && lunar.getTimeInGanZhi().equals(timeGanZhi)) { l.add(solar); break; @@ -318,7 +331,7 @@ public class Solar{ * * @return true/false 闰年/非闰年 */ - public boolean isLeapYear(){ + public boolean isLeapYear() { return SolarUtil.isLeapYear(year); } @@ -327,8 +340,8 @@ public class Solar{ * * @return 0123456 */ - public int getWeek(){ - return calendar.get(Calendar.DAY_OF_WEEK)-1; + public int getWeek() { + return calendar.get(Calendar.DAY_OF_WEEK) - 1; } /** @@ -336,7 +349,7 @@ public class Solar{ * * @return 日一二三四五六 */ - public String getWeekInChinese(){ + public String getWeekInChinese() { return SolarUtil.WEEK[getWeek()]; } @@ -345,19 +358,19 @@ public class Solar{ * * @return 劳动节等 */ - public List getFestivals(){ + public List getFestivals() { List l = new ArrayList(); //获取几月几日对应的节日 - String f = SolarUtil.FESTIVAL.get(month+"-"+day); - if(null!=f){ + String f = SolarUtil.FESTIVAL.get(month + "-" + day); + if (null != f) { l.add(f); } //计算几月第几个星期几对应的节日 - int weeks = (int)Math.ceil(day/7D); + int weeks = (int) Math.ceil(day / 7D); //星期几,0代表星期天 int week = getWeek(); - f = SolarUtil.WEEK_FESTIVAL.get(month+"-"+weeks+"-"+week); - if(null!=f){ + f = SolarUtil.WEEK_FESTIVAL.get(month + "-" + weeks + "-" + week); + if (null != f) { l.add(f); } return l; @@ -368,10 +381,10 @@ public class Solar{ * * @return 非正式的节日列表,如中元节 */ - public List getOtherFestivals(){ + public List getOtherFestivals() { List l = new ArrayList(); - List fs = SolarUtil.OTHER_FESTIVAL.get(month+"-"+day); - if(null!=fs){ + List fs = SolarUtil.OTHER_FESTIVAL.get(month + "-" + day); + if (null != fs) { l.addAll(fs); } return l; @@ -383,7 +396,7 @@ public class Solar{ * @return 星座 * @deprecated 使用getXingZuo */ - public String getXingzuo(){ + public String getXingzuo() { return getXingZuo(); } @@ -392,30 +405,30 @@ public class Solar{ * * @return 星座 */ - public String getXingZuo(){ + public String getXingZuo() { int index = 11; - int y = month*100+day; - if(y>=321&&y<=419){ + int y = month * 100 + day; + if (y >= 321 && y <= 419) { index = 0; - }else if(y>=420&&y<=520){ + } else if (y >= 420 && y <= 520) { index = 1; - }else if(y>=521&&y<=621){ + } else if (y >= 521 && y <= 621) { index = 2; - }else if(y>=622&&y<=722){ + } else if (y >= 622 && y <= 722) { index = 3; - }else if(y>=723&&y<=822){ + } else if (y >= 723 && y <= 822) { index = 4; - }else if(y>=823&&y<=922){ + } else if (y >= 823 && y <= 922) { index = 5; - }else if(y>=923&&y<=1023){ + } else if (y >= 923 && y <= 1023) { index = 6; - }else if(y>=1024&&y<=1122){ + } else if (y >= 1024 && y <= 1122) { index = 7; - }else if(y>=1123&&y<=1221){ + } else if (y >= 1123 && y <= 1221) { index = 8; - }else if(y>=1222||y<=119){ + } else if (y >= 1222 || y <= 119) { index = 9; - }else if(y<=218){ + } else if (y <= 218) { index = 10; } return SolarUtil.XINGZUO[index]; @@ -426,7 +439,7 @@ public class Solar{ * * @return 如2015 */ - public int getYear(){ + public int getYear() { return year; } @@ -435,7 +448,7 @@ public class Solar{ * * @return 1到12 */ - public int getMonth(){ + public int getMonth() { return month; } @@ -444,7 +457,7 @@ public class Solar{ * * @return 1到31之间的数字 */ - public int getDay(){ + public int getDay() { return day; } @@ -453,7 +466,7 @@ public class Solar{ * * @return 0到23之间的数字 */ - public int getHour(){ + public int getHour() { return hour; } @@ -462,7 +475,7 @@ public class Solar{ * * @return 0到59之间的数字 */ - public int getMinute(){ + public int getMinute() { return minute; } @@ -471,29 +484,31 @@ public class Solar{ * * @return 0到59之间的数字 */ - public int getSecond(){ + public int getSecond() { return second; } /** * 获取农历 + * * @return 农历 */ - public Lunar getLunar(){ + public Lunar getLunar() { return new Lunar(calendar.getTime()); } /** * 获取儒略日 + * * @return 儒略日 */ - public double getJulianDay(){ + public double getJulianDay() { int y = this.year; int m = this.month; - double d = this.day + ((this.second *1D / 60 + this.minute) / 60 + this.hour) / 24; + double d = this.day + ((this.second * 1D / 60 + this.minute) / 60 + this.hour) / 24; int n = 0; boolean g = false; - if (y * 372 + m * 31 + (int)d >= 588829) { + if (y * 372 + m * 31 + (int) d >= 588829) { g = true; } if (m <= 2) { @@ -501,10 +516,10 @@ public class Solar{ y--; } if (g) { - n = (int)(y * 1D / 100); - n = 2 - n + (int)(n * 1D / 4); + n = (int) (y * 1D / 100); + n = 2 - n + (int) (n * 1D / 4); } - return (int)(365.25 * (y + 4716)) + (int)(30.6001 * (m + 1)) + d + n - 1524.5; + return (int) (365.25 * (y + 4716)) + (int) (30.6001 * (m + 1)) + d + n - 1524.5; } /** @@ -512,39 +527,39 @@ public class Solar{ * * @return 日历 */ - public Calendar getCalendar(){ + public Calendar getCalendar() { return calendar; } @Override - public String toString(){ + public String toString() { return toYmd(); } - public String toYmd(){ + public String toYmd() { return String.format("%04d-%02d-%02d", year, month, day); } - public String toYmdHms(){ - return toYmd()+" "+String.format("%02d:%02d:%02d", hour, minute, second); + public String toYmdHms() { + return toYmd() + " " + String.format("%02d:%02d:%02d", hour, minute, second); } - public String toFullString(){ + public String toFullString() { StringBuilder s = new StringBuilder(); s.append(toYmdHms()); - if(isLeapYear()){ + if (isLeapYear()) { s.append(" "); s.append("闰年"); } s.append(" "); s.append("星期"); s.append(getWeekInChinese()); - for(String f:getFestivals()){ + for (String f : getFestivals()) { s.append(" ("); s.append(f); s.append(")"); } - for(String f:getOtherFestivals()){ + for (String f : getOtherFestivals()) { s.append(" ("); s.append(f); s.append(")"); @@ -557,43 +572,43 @@ public class Solar{ /** * 获取往后推几天的阳历日期,如果要往前推,则天数用负数 + * * @param days 天数 * @return 阳历日期 */ - public Solar next(int days){ - return next(days,false); + public Solar next(int days) { + return next(days, false); } /** * 取往后推几天的阳历日期,如果要往前推,则天数用负数 - * @param days 天数 + * + * @param days 天数 * @param onlyWorkday 是否仅限工作日 * @return 阳历日期 */ @SuppressWarnings("MagicConstant") - public Solar next(int days, boolean onlyWorkday){ - Calendar c = Calendar.getInstance(); - c.set(year,month-1,day,hour,minute,second); - c.set(Calendar.MILLISECOND,0); - if(0!=days) { - if(!onlyWorkday){ - c.add(Calendar.DATE,days); - }else { + public Solar next(int days, boolean onlyWorkday) { + Calendar c = ExactDate.fromYmdHms(year, month, day, hour, minute, second); + if (0 != days) { + if (!onlyWorkday) { + c.add(Calendar.DATE, days); + } else { int rest = Math.abs(days); int add = days < 1 ? -1 : 1; while (rest > 0) { c.add(Calendar.DATE, add); boolean work = true; - Holiday holiday = HolidayUtil.getHoliday(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1, c.get(Calendar.DAY_OF_MONTH)); - if(null==holiday){ + Holiday holiday = HolidayUtil.getHoliday(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH)); + if (null == holiday) { int week = c.get(Calendar.DAY_OF_WEEK); - if(1==week||7==week){ + if (1 == week || 7 == week) { work = false; } - }else{ + } else { work = holiday.isWork(); } - if(work){ + if (work) { rest--; } } diff --git a/src/main/java/com/nlf/calendar/SolarHalfYear.java b/src/main/java/com/nlf/calendar/SolarHalfYear.java index 4017253..f2933d4 100644 --- a/src/main/java/com/nlf/calendar/SolarHalfYear.java +++ b/src/main/java/com/nlf/calendar/SolarHalfYear.java @@ -11,46 +11,51 @@ import java.util.List; * @author 6tail */ public class SolarHalfYear { - /** 年 */ + /** + * 年 + */ private int year; - /** 月 */ + /** + * 月 + */ private int month; - /** 半年的月数 */ + /** + * 半年的月数 + */ public static final int MONTH_COUNT = 6; /** * 默认当月 */ - public SolarHalfYear(){ + public SolarHalfYear() { this(new Date()); } /** * 通过日期初始化 */ - public SolarHalfYear(Date date){ - Calendar c = Calendar.getInstance(); - c.setTime(date); + public SolarHalfYear(Date date) { + Calendar c = ExactDate.fromDate(date); year = c.get(Calendar.YEAR); - month = c.get(Calendar.MONTH)+1; + month = c.get(Calendar.MONTH) + 1; } /** * 通过日历初始化 */ - public SolarHalfYear(Calendar calendar){ + public SolarHalfYear(Calendar calendar) { year = calendar.get(Calendar.YEAR); - month = calendar.get(Calendar.MONTH)+1; + month = calendar.get(Calendar.MONTH) + 1; } /** * 通过年月初始化 * - * @param year 年 + * @param year 年 * @param month 月 */ - public SolarHalfYear(int year,int month){ + public SolarHalfYear(int year, int month) { this.year = year; this.month = month; } @@ -61,7 +66,7 @@ public class SolarHalfYear { * @param date 日期 * @return 阳历半年 */ - public static SolarHalfYear fromDate(Date date){ + public static SolarHalfYear fromDate(Date date) { return new SolarHalfYear(date); } @@ -71,19 +76,19 @@ public class SolarHalfYear { * @param calendar 日历 * @return 阳历半年 */ - public static SolarHalfYear fromCalendar(Calendar calendar){ + public static SolarHalfYear fromCalendar(Calendar calendar) { return new SolarHalfYear(calendar); } /** * 通过指定年月获取阳历半年 * - * @param year 年 + * @param year 年 * @param month 月 * @return 阳历半年 */ - public static SolarHalfYear fromYm(int year,int month){ - return new SolarHalfYear(year,month); + public static SolarHalfYear fromYm(int year, int month) { + return new SolarHalfYear(year, month); } /** @@ -91,7 +96,7 @@ public class SolarHalfYear { * * @return 年 */ - public int getYear(){ + public int getYear() { return year; } @@ -100,53 +105,55 @@ public class SolarHalfYear { * * @return 月 */ - public int getMonth(){ + public int getMonth() { return month; } /** * 获取当月是第几半年 + * * @return 半年序号,从1开始 */ - public int getIndex(){ - return (int)Math.ceil(month*1D/MONTH_COUNT); + public int getIndex() { + return (int) Math.ceil(month * 1D / MONTH_COUNT); } /** * 半年推移 + * * @param halfYears 推移的半年数,负数为倒推 * @return 推移后的半年 */ @SuppressWarnings("MagicConstant") - public SolarHalfYear next(int halfYears){ - if(0==halfYears){ - return new SolarHalfYear(year,month); + public SolarHalfYear next(int halfYears) { + if (0 == halfYears) { + return new SolarHalfYear(year, month); } - Calendar c = Calendar.getInstance(); - c.set(year,month-1,1); - c.add(Calendar.MONTH,MONTH_COUNT*halfYears); + Calendar c = ExactDate.fromYmd(year, month, 1); + c.add(Calendar.MONTH, MONTH_COUNT * halfYears); return new SolarHalfYear(c); } /** * 获取本半年的月份 + * * @return 本半年的月份列表 */ - public List getMonths(){ + public List getMonths() { List l = new ArrayList(); - int index = getIndex()-1; - for(int i=0;i getDays(){ + public List getDays() { List l = new ArrayList(31); - Solar d = new Solar(year,month,1); + Solar d = new Solar(year, month, 1); l.add(d); - int days = SolarUtil.getDaysOfMonth(year,month); - for(int i = 1;i getMonths(){ + public List getMonths() { List l = new ArrayList(); - int index = getIndex()-1; - for(int i=0;i0; - while(0!=n){ - c.add(Calendar.DATE,plus?7:-7); - week = new SolarWeek(c,start); + boolean plus = n > 0; + while (0 != n) { + c.add(Calendar.DATE, plus ? 7 : -7); + week = new SolarWeek(c, start); int weekMonth = week.getMonth(); - if(month!=weekMonth){ + if (month != weekMonth) { int index = week.getIndex(); - if(plus){ - if(1==index){ + if (plus) { + if (1 == index) { Solar firstDay = week.getFirstDay(); - week = new SolarWeek(firstDay.getYear(),firstDay.getMonth(),firstDay.getDay(),start); + week = new SolarWeek(firstDay.getYear(), firstDay.getMonth(), firstDay.getDay(), start); weekMonth = week.getMonth(); - }else{ - c.set(week.getYear(),week.getMonth()-1,1); - week = new SolarWeek(c,start); + } else { + c = ExactDate.fromYmd(week.getYear(), week.getMonth(), 1); + week = new SolarWeek(c, start); } - }else{ - int size = SolarUtil.getWeeksOfMonth(week.getYear(),week.getMonth(),start); - if(size==index){ + } else { + int size = SolarUtil.getWeeksOfMonth(week.getYear(), week.getMonth(), start); + if (size == index) { Solar firstDay = week.getFirstDay(); Solar lastDay = firstDay.next(6); - week = new SolarWeek(lastDay.getYear(),lastDay.getMonth(),lastDay.getDay(),start); + week = new SolarWeek(lastDay.getYear(), lastDay.getMonth(), lastDay.getDay(), start); weekMonth = week.getMonth(); - }else{ - c.set(week.getYear(),week.getMonth()-1,SolarUtil.getDaysOfMonth(week.getYear(),week.getMonth())); - week = new SolarWeek(c,start); + } else { + c = ExactDate.fromYmd(week.getYear(), week.getMonth(), SolarUtil.getDaysOfMonth(week.getYear(), week.getMonth())); + week = new SolarWeek(c, start); } } month = weekMonth; } - n-=plus?1:-1; + n -= plus ? 1 : -1; } return week; - }else{ - Calendar c = Calendar.getInstance(); - c.set(year,month-1,day); - c.add(Calendar.DATE,weeks*7); - return new SolarWeek(c,start); + } else { + Calendar c = ExactDate.fromYmd(year, month, day); + c.add(Calendar.DATE, weeks * 7); + return new SolarWeek(c, start); } } /** * 获取本周第一天的阳历日期(可能跨月) + * * @return 本周第一天的阳历日期 */ @SuppressWarnings("MagicConstant") - public Solar getFirstDay(){ - Calendar c = Calendar.getInstance(); - c.set(year,month-1,day); - int week = c.get(Calendar.DAY_OF_WEEK)-1; + public Solar getFirstDay() { + Calendar c = ExactDate.fromYmd(year, month, day); + int week = c.get(Calendar.DAY_OF_WEEK) - 1; int prev = week - start; - if(prev<0){ + if (prev < 0) { prev += 7; } - c.add(Calendar.DATE,-prev); + c.add(Calendar.DATE, -prev); return new Solar(c); } /** * 获取本周第一天的阳历日期(仅限当月) + * * @return 本周第一天的阳历日期 */ - public Solar getFirstDayInMonth(){ + public Solar getFirstDayInMonth() { List days = getDays(); - for(Solar day:days){ - if(month==day.getMonth()){ + for (Solar day : days) { + if (month == day.getMonth()) { return day; } } @@ -246,13 +256,14 @@ public class SolarWeek { /** * 获取本周的阳历日期列表(可能跨月) + * * @return 本周的阳历日期列表 */ - public List getDays(){ + public List getDays() { Solar firstDay = getFirstDay(); List l = new ArrayList(); l.add(firstDay); - for(int i = 1;i<7;i++){ + for (int i = 1; i < 7; i++) { l.add(firstDay.next(i)); } return l; @@ -260,13 +271,14 @@ public class SolarWeek { /** * 获取本周的阳历日期列表(仅限当月) + * * @return 本周的阳历日期列表(仅限当月) */ - public List getDaysInMonth(){ + public List getDaysInMonth() { List days = this.getDays(); List l = new ArrayList(); - for(Solar day:days){ - if(month!=day.getMonth()){ + for (Solar day : days) { + if (month != day.getMonth()) { continue; } l.add(day); @@ -275,11 +287,11 @@ public class SolarWeek { } @Override - public String toString(){ - return year+"."+month+"."+getIndex(); + public String toString() { + return year + "." + month + "." + getIndex(); } - public String toFullString(){ - return year+"年"+month+"月第"+getIndex()+"周"; + public String toFullString() { + return year + "年" + month + "月第" + getIndex() + "周"; } } diff --git a/src/main/java/com/nlf/calendar/SolarYear.java b/src/main/java/com/nlf/calendar/SolarYear.java index df0778e..1488f53 100644 --- a/src/main/java/com/nlf/calendar/SolarYear.java +++ b/src/main/java/com/nlf/calendar/SolarYear.java @@ -9,35 +9,37 @@ import java.util.List; * 阳历年 * * @author 6tail - * */ -public class SolarYear{ - /** 年 */ +public class SolarYear { + /** + * 年 + */ private int year; - /** 一年的月数 */ + /** + * 一年的月数 + */ public static final int MONTH_COUNT = 12; /** * 默认当年 */ - public SolarYear(){ + public SolarYear() { this(new Date()); } /** * 通过日期初始化 */ - public SolarYear(Date date){ - Calendar c = Calendar.getInstance(); - c.setTime(date); + public SolarYear(Date date) { + Calendar c = ExactDate.fromDate(date); year = c.get(Calendar.YEAR); } /** * 通过日历初始化 */ - public SolarYear(Calendar calendar){ + public SolarYear(Calendar calendar) { year = calendar.get(Calendar.YEAR); } @@ -46,7 +48,7 @@ public class SolarYear{ * * @param year 年 */ - public SolarYear(int year){ + public SolarYear(int year) { this.year = year; } @@ -56,7 +58,7 @@ public class SolarYear{ * @param date 日期 * @return 阳历年 */ - public static SolarYear fromDate(Date date){ + public static SolarYear fromDate(Date date) { return new SolarYear(date); } @@ -66,7 +68,7 @@ public class SolarYear{ * @param calendar 日历 * @return 阳历年 */ - public static SolarYear fromCalendar(Calendar calendar){ + public static SolarYear fromCalendar(Calendar calendar) { return new SolarYear(calendar); } @@ -76,7 +78,7 @@ public class SolarYear{ * @param year 年 * @return 阳历年 */ - public static SolarYear fromYear(int year){ + public static SolarYear fromYear(int year) { return new SolarYear(year); } @@ -85,7 +87,7 @@ public class SolarYear{ * * @return 年 */ - public int getYear(){ + public int getYear() { return year; } @@ -94,11 +96,11 @@ public class SolarYear{ * * @return 阳历月列表 */ - public List getMonths(){ + public List getMonths() { List l = new ArrayList(MONTH_COUNT); - SolarMonth m = new SolarMonth(year,1); + SolarMonth m = new SolarMonth(year, 1); l.add(m); - for(int i = 1;i FESTIVAL = new HashMap(){ +public class SolarUtil { + /** + * 星期 + */ + public static final String[] WEEK = {"日", "一", "二", "三", "四", "五", "六"}; + /** + * 每月天数 + */ + public static final int[] DAYS_OF_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + /** + * 星座 + */ + public static final String[] XINGZUO = {"白羊", "金牛", "双子", "巨蟹", "狮子", "处女", "天秤", "天蝎", "射手", "摩羯", "水瓶", "双鱼"}; + /** + * 日期对应的节日 + */ + public static final Map FESTIVAL = new HashMap() { private static final long serialVersionUID = -1; + { - put("1-1","元旦节"); - put("2-14","情人节"); - put("3-8","妇女节"); - put("3-12","植树节"); - put("3-15","消费者权益日"); - put("4-1","愚人节"); - put("5-1","劳动节"); - put("5-4","青年节"); - put("6-1","儿童节"); - put("7-1","建党节"); - put("8-1","建军节"); - put("9-10","教师节"); - put("10-1","国庆节"); - put("12-24","平安夜"); - put("12-25","圣诞节"); + put("1-1", "元旦节"); + put("2-14", "情人节"); + put("3-8", "妇女节"); + put("3-12", "植树节"); + put("3-15", "消费者权益日"); + put("4-1", "愚人节"); + put("5-1", "劳动节"); + put("5-4", "青年节"); + put("6-1", "儿童节"); + put("7-1", "建党节"); + put("8-1", "建军节"); + put("9-10", "教师节"); + put("10-1", "国庆节"); + put("12-24", "平安夜"); + put("12-25", "圣诞节"); } }; - /** 几月第几个星期几对应的节日 */ - public static final Map WEEK_FESTIVAL = new HashMap(){ + /** + * 几月第几个星期几对应的节日 + */ + public static final Map WEEK_FESTIVAL = new HashMap() { private static final long serialVersionUID = -1; + { - put("5-2-0","母亲节"); - put("6-3-0","父亲节"); - put("11-4-4","感恩节"); + put("5-2-0", "母亲节"); + put("6-3-0", "父亲节"); + put("11-4-4", "感恩节"); } }; - /** 日期对应的非正式节日 */ - public static final Map> OTHER_FESTIVAL = new HashMap>(){ + /** + * 日期对应的非正式节日 + */ + public static final Map> OTHER_FESTIVAL = new HashMap>() { private static final long serialVersionUID = -1; + { - put("1-8",Collections.nCopies(1,"周恩来逝世纪念日")); - put("1-10",Arrays.asList("中国人民警察节","中国公安110宣传日")); - put("1-21",Collections.nCopies(1,"列宁逝世纪念日")); - put("1-26",Collections.nCopies(1,"国际海关日")); - put("2-2",Collections.nCopies(1,"世界湿地日")); - put("2-4",Collections.nCopies(1,"世界抗癌日")); - put("2-7",Collections.nCopies(1,"京汉铁路罢工纪念")); - put("2-10",Collections.nCopies(1,"国际气象节")); - put("2-19",Collections.nCopies(1,"邓小平逝世纪念日")); - put("2-21",Collections.nCopies(1,"国际母语日")); - put("2-24",Collections.nCopies(1,"第三世界青年日")); - put("3-1",Collections.nCopies(1,"国际海豹日")); - put("3-3",Collections.nCopies(1,"全国爱耳日")); - put("3-5",Arrays.asList("周恩来诞辰纪念日","中国青年志愿者服务日")); - put("3-6",Collections.nCopies(1,"世界青光眼日")); - put("3-12",Collections.nCopies(1,"孙中山逝世纪念日")); - put("3-14",Collections.nCopies(1,"马克思逝世纪念日")); - put("3-17",Collections.nCopies(1,"国际航海日")); - put("3-18",Collections.nCopies(1,"全国科技人才活动日")); - put("3-21",Arrays.asList("世界森林日","世界睡眠日")); - put("3-22",Collections.nCopies(1,"世界水日")); - put("3-23",Collections.nCopies(1,"世界气象日")); - put("3-24",Collections.nCopies(1,"世界防治结核病日")); - put("4-2",Collections.nCopies(1,"国际儿童图书日")); - put("4-7",Collections.nCopies(1,"世界卫生日")); - put("4-22",Collections.nCopies(1,"列宁诞辰纪念日")); - put("4-23",Collections.nCopies(1,"世界图书和版权日")); - put("4-26",Collections.nCopies(1,"世界知识产权日")); - put("5-3",Collections.nCopies(1,"世界新闻自由日")); - put("5-5",Collections.nCopies(1,"马克思诞辰纪念日")); - put("5-8",Collections.nCopies(1,"世界红十字日")); - put("5-11",Collections.nCopies(1,"世界肥胖日")); - put("5-23",Collections.nCopies(1,"世界读书日")); - put("5-27",Collections.nCopies(1,"上海解放日")); - put("5-31",Collections.nCopies(1,"世界无烟日")); - put("6-5",Collections.nCopies(1,"世界环境日")); - put("6-6",Collections.nCopies(1,"全国爱眼日")); - put("6-8",Collections.nCopies(1,"世界海洋日")); - put("6-11",Collections.nCopies(1,"中国人口日")); - put("6-14",Collections.nCopies(1,"世界献血日")); - put("7-1",Collections.nCopies(1,"香港回归纪念日")); - put("7-7",Collections.nCopies(1,"中国人民抗日战争纪念日")); - put("7-11",Collections.nCopies(1,"世界人口日")); - put("8-5",Collections.nCopies(1,"恩格斯逝世纪念日")); - put("8-6",Collections.nCopies(1,"国际电影节")); - put("8-12",Collections.nCopies(1,"国际青年日")); - put("8-22",Collections.nCopies(1,"邓小平诞辰纪念日")); - put("9-3",Collections.nCopies(1,"中国抗日战争胜利纪念日")); - put("9-8",Collections.nCopies(1,"世界扫盲日")); - put("9-9",Collections.nCopies(1,"毛泽东逝世纪念日")); - put("9-14",Collections.nCopies(1,"世界清洁地球日")); - put("9-18",Collections.nCopies(1,"九一八事变纪念日")); - put("9-20",Collections.nCopies(1,"全国爱牙日")); - put("9-21",Collections.nCopies(1,"国际和平日")); - put("9-27",Collections.nCopies(1,"世界旅游日")); - put("10-4",Collections.nCopies(1,"世界动物日")); - put("10-10",Collections.nCopies(1,"辛亥革命纪念日")); - put("10-13",Collections.nCopies(1,"中国少年先锋队诞辰日")); - put("10-25",Collections.nCopies(1,"抗美援朝纪念日")); - put("11-12",Collections.nCopies(1,"孙中山诞辰纪念日")); - put("11-17",Collections.nCopies(1,"国际大学生节")); - put("11-28",Collections.nCopies(1,"恩格斯诞辰纪念日")); - put("12-1",Collections.nCopies(1,"世界艾滋病日")); - put("12-12",Collections.nCopies(1,"西安事变纪念日")); - put("12-13",Collections.nCopies(1,"南京大屠杀纪念日")); - put("12-26",Collections.nCopies(1,"毛泽东诞辰纪念日")); + put("1-8", Collections.nCopies(1, "周恩来逝世纪念日")); + put("1-10", Arrays.asList("中国人民警察节", "中国公安110宣传日")); + put("1-21", Collections.nCopies(1, "列宁逝世纪念日")); + put("1-26", Collections.nCopies(1, "国际海关日")); + put("2-2", Collections.nCopies(1, "世界湿地日")); + put("2-4", Collections.nCopies(1, "世界抗癌日")); + put("2-7", Collections.nCopies(1, "京汉铁路罢工纪念")); + put("2-10", Collections.nCopies(1, "国际气象节")); + put("2-19", Collections.nCopies(1, "邓小平逝世纪念日")); + put("2-21", Collections.nCopies(1, "国际母语日")); + put("2-24", Collections.nCopies(1, "第三世界青年日")); + put("3-1", Collections.nCopies(1, "国际海豹日")); + put("3-3", Collections.nCopies(1, "全国爱耳日")); + put("3-5", Arrays.asList("周恩来诞辰纪念日", "中国青年志愿者服务日")); + put("3-6", Collections.nCopies(1, "世界青光眼日")); + put("3-12", Collections.nCopies(1, "孙中山逝世纪念日")); + put("3-14", Collections.nCopies(1, "马克思逝世纪念日")); + put("3-17", Collections.nCopies(1, "国际航海日")); + put("3-18", Collections.nCopies(1, "全国科技人才活动日")); + put("3-21", Arrays.asList("世界森林日", "世界睡眠日")); + put("3-22", Collections.nCopies(1, "世界水日")); + put("3-23", Collections.nCopies(1, "世界气象日")); + put("3-24", Collections.nCopies(1, "世界防治结核病日")); + put("4-2", Collections.nCopies(1, "国际儿童图书日")); + put("4-7", Collections.nCopies(1, "世界卫生日")); + put("4-22", Collections.nCopies(1, "列宁诞辰纪念日")); + put("4-23", Collections.nCopies(1, "世界图书和版权日")); + put("4-26", Collections.nCopies(1, "世界知识产权日")); + put("5-3", Collections.nCopies(1, "世界新闻自由日")); + put("5-5", Collections.nCopies(1, "马克思诞辰纪念日")); + put("5-8", Collections.nCopies(1, "世界红十字日")); + put("5-11", Collections.nCopies(1, "世界肥胖日")); + put("5-23", Collections.nCopies(1, "世界读书日")); + put("5-27", Collections.nCopies(1, "上海解放日")); + put("5-31", Collections.nCopies(1, "世界无烟日")); + put("6-5", Collections.nCopies(1, "世界环境日")); + put("6-6", Collections.nCopies(1, "全国爱眼日")); + put("6-8", Collections.nCopies(1, "世界海洋日")); + put("6-11", Collections.nCopies(1, "中国人口日")); + put("6-14", Collections.nCopies(1, "世界献血日")); + put("7-1", Collections.nCopies(1, "香港回归纪念日")); + put("7-7", Collections.nCopies(1, "中国人民抗日战争纪念日")); + put("7-11", Collections.nCopies(1, "世界人口日")); + put("8-5", Collections.nCopies(1, "恩格斯逝世纪念日")); + put("8-6", Collections.nCopies(1, "国际电影节")); + put("8-12", Collections.nCopies(1, "国际青年日")); + put("8-22", Collections.nCopies(1, "邓小平诞辰纪念日")); + put("9-3", Collections.nCopies(1, "中国抗日战争胜利纪念日")); + put("9-8", Collections.nCopies(1, "世界扫盲日")); + put("9-9", Collections.nCopies(1, "毛泽东逝世纪念日")); + put("9-14", Collections.nCopies(1, "世界清洁地球日")); + put("9-18", Collections.nCopies(1, "九一八事变纪念日")); + put("9-20", Collections.nCopies(1, "全国爱牙日")); + put("9-21", Collections.nCopies(1, "国际和平日")); + put("9-27", Collections.nCopies(1, "世界旅游日")); + put("10-4", Collections.nCopies(1, "世界动物日")); + put("10-10", Collections.nCopies(1, "辛亥革命纪念日")); + put("10-13", Collections.nCopies(1, "中国少年先锋队诞辰日")); + put("10-25", Collections.nCopies(1, "抗美援朝纪念日")); + put("11-12", Collections.nCopies(1, "孙中山诞辰纪念日")); + put("11-17", Collections.nCopies(1, "国际大学生节")); + put("11-28", Collections.nCopies(1, "恩格斯诞辰纪念日")); + put("12-1", Collections.nCopies(1, "世界艾滋病日")); + put("12-12", Collections.nCopies(1, "西安事变纪念日")); + put("12-13", Collections.nCopies(1, "南京大屠杀纪念日")); + put("12-26", Collections.nCopies(1, "毛泽东诞辰纪念日")); } }; - protected SolarUtil(){} + protected SolarUtil() { + } /** * 是否闰年 @@ -125,22 +143,22 @@ public class SolarUtil{ * @param year 年 * @return true/false 闰年/非闰年 */ - public static boolean isLeapYear(int year){ - return (year%4 == 0 && year%100 != 0) || (year%400 == 0); + public static boolean isLeapYear(int year) { + return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } /** * 获取某年某月有多少天 * - * @param year 年 + * @param year 年 * @param month 月 * @return 天数 */ - public static int getDaysOfMonth(int year,int month){ - int m = month-1; + public static int getDaysOfMonth(int year, int month) { + int m = month - 1; int d = DAYS_OF_MONTH[m]; //公历闰年2月多一天 - if(m==Calendar.FEBRUARY&&isLeapYear(year)){ + if (m == Calendar.FEBRUARY && isLeapYear(year)) { d++; } return d; @@ -149,17 +167,14 @@ public class SolarUtil{ /** * 获取某年某月有多少周 * - * @param year 年 + * @param year 年 * @param month 月 * @param start 星期几作为一周的开始,1234560分别代表星期一至星期天 * @return 周数 */ - @SuppressWarnings("MagicConstant") - public static int getWeeksOfMonth(int year,int month,int start){ - int days = getDaysOfMonth(year,month); - Calendar c = Calendar.getInstance(); - c.set(year,month-1,1); - int week = c.get(Calendar.DAY_OF_WEEK)-1; - return (int)Math.ceil((days+week-start)*1D/WEEK.length); + public static int getWeeksOfMonth(int year, int month, int start) { + int days = getDaysOfMonth(year, month); + int week = ExactDate.fromYmd(year, month, 1).get(Calendar.DAY_OF_WEEK) - 1; + return (int) Math.ceil((days + week - start) * 1D / WEEK.length); } } diff --git a/src/test/java/test/SolarTest.java b/src/test/java/test/SolarTest.java index 7ee4e2c..182fa60 100644 --- a/src/test/java/test/SolarTest.java +++ b/src/test/java/test/SolarTest.java @@ -26,6 +26,24 @@ public class SolarTest { Assert.assertEquals("二〇二〇年闰四月初二",solar.getLunar().toString()); } + @Test + public void test6(){ + Solar solar = new Solar(11,1,1); + Assert.assertEquals("一〇年腊月初八",solar.getLunar().toString()); + } + + @Test + public void test7(){ + Solar solar = new Solar(11,3,1); + Assert.assertEquals("一一年二月初八",solar.getLunar().toString()); + } + + @Test + public void test9(){ + Solar solar = new Solar(26,4,13); + Assert.assertEquals("二六年三月初八",solar.getLunar().toString()); + } + @Test public void testNext(){ Solar date = new Solar(2020,1,23);