Java編寫多功能萬(wàn)年歷程序的實(shí)例分享
這里的萬(wàn)年歷制作主要用到了Calendar類和GregorianCalendar類,我們先來(lái)回顧一下基礎(chǔ)知識(shí):
基礎(chǔ)部分
一、Calendar類。
1,主要字段:
YEAR(年份)、MONTH(月份從0開(kāi)始)、DATE(一月的某天)、HOUR(指示上午或下午的小時(shí))、HOUR_F_DAY(指示一天中的小時(shí)。)、DAY_OF_WEEK (一個(gè)星期中的某天)、DAY_OF_MONTH(一個(gè)月中的某天)、DAY_OF_YEAR(一年中的某天)、DAY_OF_WEEK _IN_MONTH(一個(gè)月中的第幾個(gè)星期)、WEEK_OF_MONTH (指示當(dāng)前月中的星期數(shù))、WEEK_OF_YEAR(指示當(dāng)前年中的星期數(shù))
2,得Calendar類對(duì)象。
//通過(guò) Calendar類的靜態(tài)方法getInstance獲得。 Calendar ca = Calendar.getInstance();
3,主要方法
void set(int field,int value)//field日歷類的參數(shù),比如YEAR MONTH DATE 等... void set(int year,int month,int date)//設(shè)置年月日。 void set(int year, int month, int date, int hourOfDay, int minute)//設(shè)置年月日時(shí)分 void set(int year, int month, int date, int hourOfDay, int minute, int second)//設(shè)置年月日時(shí)分秒 void setTime(Date date);//使用給定的 Date 設(shè)置此 Calendar 的時(shí)間。 int get(int field)//返回給定日歷字段的值。如:int month = acobj.get(Calendar.MONTH); Date getTime()//返回一個(gè)表示此 Calendar 時(shí)間值的 Date 對(duì)象。 long getTimeInMillis()//返回從1970.1.1 00:00:00到該日歷的毫秒數(shù)。 void add(int field,amont);//根據(jù)日歷的規(guī)則,為給定的日歷字段添加或減去指定的時(shí)間量??杉訙p。如:caobj.add(Calendar.MONTH,1)下一個(gè)月。
二、GregorianCalendar類。
1,獲得該類對(duì)象
Calendar ca = new GregorianCalendar()//默認(rèn)當(dāng)前的時(shí)刻。 Calendar ca = new GregorianCanlendar(int year,int month,int dayOfMonth)//初始具有指定年月日的公歷類對(duì)象。 Calendar ca = new GregorianCanlendar(int year,int month,int dayOfMonth,int hourOfDay,int minute)初始具有指定年月日的公歷類對(duì)象。 Calendar ca = new GregorianCanlendar(int year,int month,int dayOfMonth,int hourOfDay,int minute,int second)//初始具有指定年月日的公歷類對(duì)象。 上邊的都是獲得默認(rèn)的語(yǔ)言環(huán)境,和默認(rèn)的時(shí)區(qū) 對(duì)象。
2,用法
用法主要繼承去父類Calendar。
實(shí)例部分
三、萬(wàn)年歷代碼
package com.via.mce.monthcalendar.utils; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; /** * 農(nóng)歷日歷。 * 將農(nóng)歷從1901年到2100年之間各年、月的大小以及歷年節(jié)氣保存,然后基于這些數(shù)據(jù)進(jìn)行計(jì)算。 * * 新增了幾個(gè)用于農(nóng)歷的常量屬性字段,可以使用get()方法獲取日歷對(duì)應(yīng)的值; * 農(nóng)歷年、月、日還可以使用set()/add()/roll()方法設(shè)置,其他農(nóng)歷屬性自動(dòng)計(jì)算; * 另外,還提供了getChinese(int field)方法用于獲得農(nóng)歷的中文文字(僅適用于農(nóng)歷屬性和星期)。 * <ul> * <li>CHINESE_YEAR - 農(nóng)歷年</li> * <li>CHINESE_MONTH - 農(nóng)歷月</li> * <li>CHINESE_DATE - 農(nóng)歷日</li> * <li>CHINESE_SECTIONAL_TERM - 當(dāng)月的節(jié)氣</li> * <li>CHINESE_PRINCIPLE_TERM - 當(dāng)月的中氣</li> * <li>CHINESE_HEAVENLY_STEM - 農(nóng)歷年的天干</li> * <li>CHINESE_EARTHLY_BRANCH - 農(nóng)歷年的地支</li> * <li>CHINESE_ZODIAC - 農(nóng)歷年的屬相</li> * <li>CHINESE_TERM_OR_DATE - 如果當(dāng)天存在一個(gè)節(jié)氣則指示節(jié)氣,否則如果當(dāng)天是初一則指示農(nóng)歷月,否則指示農(nóng)歷日</li> * </ul> * 注意: * 由于Calendar類的設(shè)定,公歷月份從0起始。所有方法都遵循了這一約定。 * 但所有的農(nóng)歷屬性從1起始。即使是在Calendar提供的方法中,農(nóng)歷月也是從1起始的,并以負(fù)數(shù)表示閏月。 * clear()方法在某些情況下會(huì)導(dǎo)致農(nóng)歷和公歷日期不對(duì)應(yīng)或是不能達(dá)到預(yù)期的重置效果,應(yīng)盡量避免使用。 * 使用getSimpleDateString()獲得公歷日期字符串時(shí),公歷月已經(jīng)修正; * 使用getSimpleChineseDateString()獲得農(nóng)歷日期字符串時(shí),農(nóng)歷閏月以*表示。 * * @version 0.12 2011-9-5 * <blockquote>修復(fù)一個(gè)當(dāng)使用農(nóng)歷正月日期初始化日歷時(shí)陷入死循環(huán)的問(wèn)題。</blockquote> * @version 0.11 2009-12-27 * <blockquote>修復(fù)了獲取中文農(nóng)歷時(shí)未計(jì)算農(nóng)歷日期的問(wèn)題; * 加入一個(gè)字段CHINESE_TERM_OR_DATE用于模仿臺(tái)歷的顯示方式:如果當(dāng)天有節(jié)氣則指示節(jié)氣,如果是初一指示農(nóng)歷月, * 否則指示農(nóng)歷日。</blockquote> * @version 0.10 2009-12-22 */ public final class ChineseCalendar extends GregorianCalendar { private static final long serialVersionUID = 8L; /** 農(nóng)歷年 */ public static final int CHINESE_YEAR = 801; /** 農(nóng)歷月 */ public static final int CHINESE_MONTH = 802; /** 農(nóng)歷日 */ public static final int CHINESE_DATE = 803; /** 當(dāng)月的節(jié)氣對(duì)應(yīng)的公歷日(前一個(gè)節(jié)氣) */ public static final int CHINESE_SECTIONAL_TERM = 804; /** 當(dāng)月的中氣對(duì)應(yīng)的公歷日(后一個(gè)節(jié)氣) */ public static final int CHINESE_PRINCIPLE_TERM = 805; /** 天干 */ public static final int CHINESE_HEAVENLY_STEM = 806; /** 地支 */ public static final int CHINESE_EARTHLY_BRANCH = 807; /** 農(nóng)歷年的屬相(生肖) */ public static final int CHINESE_ZODIAC = 808; /** 節(jié)氣或者農(nóng)歷日 */ public static final int CHINESE_TERM_OR_DATE = 888; // add by skywang /** 農(nóng)歷節(jié)日 */ public static final int LUNAR_FESTIVAL = 809; /** 陽(yáng)歷節(jié)日 */ public static final int SOLAR_FESTIVAL = 810; /** 節(jié)氣 */ public static final int CHINESE_TERM = 811; /** 月或者農(nóng)歷日 */ public static final int CHINESE_MONTH_OR_DATE = 812; /** 節(jié)日 或 節(jié)氣 或 農(nóng)歷日 */ public static final int FESTIVAL_OR_TERM_OR_DATE = 813; private int chineseYear; private int chineseMonth; // 1起始,負(fù)數(shù)表示閏月 private int chineseDate; private int sectionalTerm; // 當(dāng)月節(jié)氣的公歷日 private int principleTerm; // 當(dāng)月中氣的公歷日 private boolean areChineseFieldsComputed; // 農(nóng)歷日期是否已經(jīng)經(jīng)過(guò)計(jì)算確認(rèn) private boolean areSolarTermsComputed; // 節(jié)氣是否已經(jīng)經(jīng)過(guò)計(jì)算確認(rèn) private boolean lastSetChinese; // 最后設(shè)置的是不是農(nóng)歷屬性 /** 使用當(dāng)前時(shí)間構(gòu)造一個(gè)實(shí)例。 */ public ChineseCalendar() { super(); } /** 使用指定時(shí)間構(gòu)造一個(gè)實(shí)例。 */ public ChineseCalendar(Date d) { super.setTime(d); } /** 使用指定時(shí)間構(gòu)造一個(gè)實(shí)例。 */ public ChineseCalendar(Calendar c) { this(c.getTime()); } /** 使用指定公歷日期構(gòu)造一個(gè)實(shí)例。 */ public ChineseCalendar(int y, int m, int d) { super(y, m, d); } /** * 使用指定日期構(gòu)造一個(gè)實(shí)例。 * * @param isChinese * 是否為農(nóng)歷日期 * @param y * @param m * @param d */ public ChineseCalendar(boolean isChinese, int y, int m, int d) { if (isChinese) { set(CHINESE_YEAR, y); set(CHINESE_MONTH, m); set(CHINESE_DATE, d); } else { set(y, m, d); } } public void set(int field, int value) { computeIfNeed(field); if (isChineseField(field)) { // 農(nóng)歷屬性 switch (field) { case CHINESE_YEAR: chineseYear = value; break; case CHINESE_MONTH: chineseMonth = value; break; case CHINESE_DATE: chineseDate = value; break; default: throw new IllegalArgumentException("不支持的field設(shè)置:" + field); } lastSetChinese = true; } else { // 非農(nóng)歷屬性 super.set(field, value); lastSetChinese = false; } areFieldsSet = false; areChineseFieldsComputed = false; areSolarTermsComputed = false; } public int get(int field) { computeIfNeed(field); if (!isChineseField(field)) { return super.get(field); } switch (field) { case CHINESE_YEAR: return chineseYear; case CHINESE_MONTH: return chineseMonth; case CHINESE_DATE: return chineseDate; case CHINESE_SECTIONAL_TERM: return sectionalTerm; case CHINESE_PRINCIPLE_TERM: return principleTerm; case CHINESE_HEAVENLY_STEM: return (chineseYear - 4) % 10 + 1; case CHINESE_EARTHLY_BRANCH: case CHINESE_ZODIAC: return (chineseYear - 4) % 12 + 1; case CHINESE_MONTH_OR_DATE: if (get(CHINESE_DATE) == 1) { return CHINESE_MONTH; } else { return CHINESE_DATE; } case CHINESE_TERM_OR_DATE: int option; if (get(Calendar.DATE) == get(CHINESE_SECTIONAL_TERM)) { option = CHINESE_SECTIONAL_TERM; } else if (get(Calendar.DATE) == get(CHINESE_PRINCIPLE_TERM)) { option = CHINESE_PRINCIPLE_TERM; } else if (get(CHINESE_DATE) == 1) { option = CHINESE_MONTH; } else { option = CHINESE_DATE; } return option; default: throw new IllegalArgumentException("不支持的field獲?。? + field); } } public void add(int field, int amount) { computeIfNeed(field); if (!isChineseField(field)) { super.add(field, amount); lastSetChinese = false; areChineseFieldsComputed = false; areSolarTermsComputed = false; return; } switch (field) { case CHINESE_YEAR: chineseYear += amount; break; case CHINESE_MONTH: for (int i = 0; i < amount; i++) { chineseMonth = nextChineseMonth(chineseYear, chineseMonth); if (chineseMonth == 1) { chineseYear++; } } break; case CHINESE_DATE: int maxDate = daysInChineseMonth(chineseYear, chineseMonth); for (int i = 0; i < amount; i++) { chineseDate++; if (chineseDate > maxDate) { chineseDate = 1; chineseMonth = nextChineseMonth(chineseYear, chineseMonth); if (chineseMonth == 1) { chineseYear++; } maxDate = daysInChineseMonth(chineseYear, chineseMonth); } } default: throw new IllegalArgumentException("不支持的field:" + field); } lastSetChinese = true; areFieldsSet = false; areChineseFieldsComputed = false; areSolarTermsComputed = false; } public void roll(int field, int amount) { computeIfNeed(field); if (!isChineseField(field)) { super.roll(field, amount); lastSetChinese = false; areChineseFieldsComputed = false; areSolarTermsComputed = false; return; } switch (field) { case CHINESE_YEAR: chineseYear += amount; break; case CHINESE_MONTH: for (int i = 0; i < amount; i++) { chineseMonth = nextChineseMonth(chineseYear, chineseMonth); } break; case CHINESE_DATE: int maxDate = daysInChineseMonth(chineseYear, chineseMonth); for (int i = 0; i < amount; i++) { chineseDate++; if (chineseDate > maxDate) { chineseDate = 1; } } default: throw new IllegalArgumentException("不支持的field:" + field); } lastSetChinese = true; areFieldsSet = false; areChineseFieldsComputed = false; areSolarTermsComputed = false; } /** * 獲得屬性的中文,可以使用的屬性字段為DAY_OF_WEEK以及所有農(nóng)歷屬性字段。 * * @param field * @return */ public String getChinese(int field) { computeIfNeed(field); switch (field) { case CHINESE_YEAR: return getChinese(CHINESE_HEAVENLY_STEM) + getChinese(CHINESE_EARTHLY_BRANCH) + "年"; case CHINESE_MONTH: if (chineseMonth > 0) return chineseMonthNames[chineseMonth] + "月"; else return "閏" + chineseMonthNames[-chineseMonth] + "月"; case CHINESE_DATE: return chineseDateNames[chineseDate]; case CHINESE_SECTIONAL_TERM: return sectionalTermNames[get(Calendar.MONTH)]; case CHINESE_PRINCIPLE_TERM: return principleTermNames[get(Calendar.MONTH)]; case CHINESE_HEAVENLY_STEM: return stemNames[get(field)]; case CHINESE_EARTHLY_BRANCH: return branchNames[get(field)]; case CHINESE_ZODIAC: return animalNames[get(field)]; case Calendar.DAY_OF_WEEK: return chineseWeekNames[get(field)]; case CHINESE_TERM_OR_DATE: return getChinese(get(CHINESE_TERM_OR_DATE)); case LUNAR_FESTIVAL: return getLunarFestival(); case SOLAR_FESTIVAL: return getSolarFestival(); case FESTIVAL_OR_TERM_OR_DATE: return getFestivalOrTermOrDate(); // TODO CHECK case CHINESE_MONTH_OR_DATE: return getChinese(get(CHINESE_MONTH_OR_DATE)); case CHINESE_TERM: return getChineseTerm(); default: throw new IllegalArgumentException("不支持的field中文獲?。? + field); } } public String getSimpleGregorianDateString() { return new StringBuffer().append(get(YEAR)).append("-") .append(get(MONTH) + 1).append("-").append(get(DATE)) .toString(); } public String getSimpleChineseDateString() { return new StringBuffer() .append(get(CHINESE_YEAR)) .append("-") .append(get(CHINESE_MONTH) > 0 ? "" + get(CHINESE_MONTH) : "*" + (-get(CHINESE_MONTH))).append("-") .append(get(CHINESE_DATE)).toString(); } public String getChineseDateString() { return new StringBuffer().append(getChinese(CHINESE_YEAR)) .append(getChinese(CHINESE_MONTH)) .append(getChinese(CHINESE_DATE)).toString(); } public String toString() { StringBuffer buf = new StringBuffer(); buf.append(getSimpleGregorianDateString()).append(" | ") .append(getChinese(DAY_OF_WEEK)).append(" | [農(nóng)歷]") .append(getChineseDateString()).append(" ") .append(getChinese(CHINESE_ZODIAC)).append("年 ") .append(get(CHINESE_SECTIONAL_TERM)).append("日") .append(getChinese(CHINESE_SECTIONAL_TERM)).append(" ") .append(get(CHINESE_PRINCIPLE_TERM)).append("日") .append(getChinese(CHINESE_PRINCIPLE_TERM)); return buf.toString(); } /** * 判斷是不是農(nóng)歷屬性 * * @param field * @return */ private boolean isChineseField(int field) { switch (field) { case CHINESE_YEAR: case CHINESE_MONTH: case CHINESE_DATE: case CHINESE_SECTIONAL_TERM: case CHINESE_PRINCIPLE_TERM: case CHINESE_HEAVENLY_STEM: case CHINESE_EARTHLY_BRANCH: case CHINESE_ZODIAC: case CHINESE_TERM_OR_DATE: case CHINESE_MONTH_OR_DATE: return true; default: return false; } } /** * 判斷是不是與節(jié)氣有關(guān)的屬性 * * @param field * @return */ private boolean isChineseTermsField(int field) { switch (field) { case CHINESE_SECTIONAL_TERM: case CHINESE_PRINCIPLE_TERM: case CHINESE_TERM_OR_DATE: return true; default: return false; } } /** * 如果上一次設(shè)置的與這次將要設(shè)置或獲取的屬性不是同一類(農(nóng)歷/公歷), * 例如上一次設(shè)置的是農(nóng)歷而現(xiàn)在要設(shè)置或獲取公歷, * 則需要先根據(jù)之前設(shè)置的農(nóng)歷日期計(jì)算出公歷日期。 * * @param field */ private void computeIfNeed(int field) { if (isChineseField(field)) { if (!lastSetChinese && !areChineseFieldsComputed) { super.complete(); computeChineseFields(); areFieldsSet = true; areChineseFieldsComputed = true; areSolarTermsComputed = false; } if (isChineseTermsField(field) && !areSolarTermsComputed) { computeSolarTerms(); areSolarTermsComputed = true; } } else { if (lastSetChinese && !areFieldsSet) { computeGregorianFields(); super.complete(); areFieldsSet = true; areChineseFieldsComputed = true; areSolarTermsComputed = false; } } } /** * 使用農(nóng)歷日期計(jì)算出公歷日期 */ private void computeGregorianFields() { int y = chineseYear; int m = chineseMonth; int d = chineseDate; areChineseFieldsComputed = true; areFieldsSet = true; lastSetChinese = false; // 調(diào)整日期范圍 if (y < 1900) y = 1899; else if (y > 2100) y = 2101; if (m < -12) m = -12; else if (m > 12) m = 12; if (d < 1) d = 1; else if (d > 30) d = 30; int dateint = y * 10000 + Math.abs(m) * 100 + d; if (dateint < 19001111) { // 太小 set(1901, Calendar.JANUARY, 1); super.complete(); } else if (dateint > 21001201) { // 太大 set(2100, Calendar.DECEMBER, 31); super.complete(); } else { if (Math.abs(m) > 12) { m = 12; } int days = ChineseCalendar.daysInChineseMonth(y, m); if (days == 0) { m = -m; days = ChineseCalendar.daysInChineseMonth(y, m); } if (d > days) { d = days; } set(y, Math.abs(m) - 1, d); computeChineseFields(); int amount = 0; while (chineseYear != y || chineseMonth != m) { amount += daysInChineseMonth(chineseYear, chineseMonth); chineseMonth = nextChineseMonth(chineseYear, chineseMonth); if (chineseMonth == 1) { chineseYear++; } } amount += d - chineseDate; super.add(Calendar.DATE, amount); } computeChineseFields(); } /** * 使用公歷日期計(jì)算出農(nóng)歷日期 */ private void computeChineseFields() { int gregorianYear = internalGet(Calendar.YEAR); int gregorianMonth = internalGet(Calendar.MONTH) + 1; int gregorianDate = internalGet(Calendar.DATE); if (gregorianYear < 1901 || gregorianYear > 2100) { return; } int startYear, startMonth, startDate; if (gregorianYear < 2000) { startYear = baseYear; startMonth = baseMonth; startDate = baseDate; chineseYear = baseChineseYear; chineseMonth = baseChineseMonth; chineseDate = baseChineseDate; } else { // 第二個(gè)對(duì)應(yīng)日,用以提高計(jì)算效率 // 公歷 2000 年 1 月 1 日,對(duì)應(yīng)農(nóng)歷 4697(1999) 年 11 月 25 日 startYear = baseYear + 99; startMonth = 1; startDate = 1; chineseYear = baseChineseYear + 99; chineseMonth = 11; chineseDate = 25; } int daysDiff = 0; // 年 for (int i = startYear; i < gregorianYear; i++) { if (isGregorianLeapYear(i)) { daysDiff += 366; // leap year } else { daysDiff += 365; } } // 月 for (int i = startMonth; i < gregorianMonth; i++) { daysDiff += daysInGregorianMonth(gregorianYear, i - 1); } // 日 daysDiff += gregorianDate - startDate; chineseDate += daysDiff; int lastDate = daysInChineseMonth(chineseYear, chineseMonth); while (chineseDate > lastDate) { chineseDate -= lastDate; chineseMonth = nextChineseMonth(chineseYear, chineseMonth); if (chineseMonth == 1) { chineseYear++; } lastDate = daysInChineseMonth(chineseYear, chineseMonth); } } /** * 計(jì)算節(jié)氣 */ private void computeSolarTerms() { int gregorianYear = internalGet(Calendar.YEAR); int gregorianMonth = internalGet(Calendar.MONTH); if (gregorianYear < 1901 || gregorianYear > 2100) { return; } sectionalTerm = sectionalTerm(gregorianYear, gregorianMonth); principleTerm = principleTerm(gregorianYear, gregorianMonth); } /* 接下來(lái)是靜態(tài)方法~ */ /** * 是否為公歷閏年 * * @param year * @return */ public static boolean isGregorianLeapYear(int year) { boolean isLeap = false; if (year % 4 == 0) { isLeap = true; } if (year % 100 == 0) { isLeap = false; } if (year % 400 == 0) { isLeap = true; } return isLeap; } /** * 計(jì)算公歷年的當(dāng)月天數(shù),公歷月從0起始! * * @param y * @param m * @return */ public static int daysInGregorianMonth(int y, int m) { int d = daysInGregorianMonth[m]; if (m == Calendar.FEBRUARY && isGregorianLeapYear(y)) { d++; // 公歷閏年二月多一天 } return d; } /** * 計(jì)算公歷年當(dāng)月的節(jié)氣,公歷月從0起始! * * @param y * @param m * @return */ public static int sectionalTerm(int y, int m) { m++; if (y < 1901 || y > 2100) { return 0; } int index = 0; int ry = y - baseYear + 1; while (ry >= sectionalTermYear[m - 1][index]) { index++; } int term = sectionalTermMap[m - 1][4 * index + ry % 4]; if ((ry == 121) && (m == 4)) { term = 5; } if ((ry == 132) && (m == 4)) { term = 5; } if ((ry == 194) && (m == 6)) { term = 6; } return term; } /** * 計(jì)算公歷年當(dāng)月的中氣,公歷月從0起始! * * @param y * @param m * @return */ public static int principleTerm(int y, int m) { m++; if (y < 1901 || y > 2100) { return 0; } int index = 0; int ry = y - baseYear + 1; while (ry >= principleTermYear[m - 1][index]) { index++; } int term = principleTermMap[m - 1][4 * index + ry % 4]; if ((ry == 171) && (m == 3)) { term = 21; } if ((ry == 181) && (m == 5)) { term = 21; } return term; } /** * 計(jì)算農(nóng)歷年的天數(shù) * * @param y * @param m * @return */ public static int daysInChineseMonth(int y, int m) { // 注意:閏月 m < 0 int index = y - baseChineseYear + baseIndex; int v = 0; int l = 0; int d = 30; if (1 <= m && m <= 8) { v = chineseMonths[2 * index]; l = m - 1; if (((v >> l) & 0x01) == 1) { d = 29; } } else if (9 <= m && m <= 12) { v = chineseMonths[2 * index + 1]; l = m - 9; if (((v >> l) & 0x01) == 1) { d = 29; } } else { v = chineseMonths[2 * index + 1]; v = (v >> 4) & 0x0F; if (v != Math.abs(m)) { d = 0; } else { d = 29; for (int i = 0; i < bigLeapMonthYears.length; i++) { if (bigLeapMonthYears[i] == index) { d = 30; break; } } } } return d; } /** * 計(jì)算農(nóng)歷的下個(gè)月 * * @param y * @param m * @return */ public static int nextChineseMonth(int y, int m) { int n = Math.abs(m) + 1; if (m > 0) { int index = y - baseChineseYear + baseIndex; int v = chineseMonths[2 * index + 1]; v = (v >> 4) & 0x0F; if (v == m) { n = -m; } } if (n == 13) { n = 1; } return n; } /* 日歷第一天的日期 */ private static final int baseYear = 1901; private static final int baseMonth = 1; private static final int baseDate = 1; private static final int baseIndex = 0; private static final int baseChineseYear = 1900; private static final int baseChineseMonth = 11; private static final int baseChineseDate = 11; /* 中文字符串 */ private static final String[] chineseWeekNames = { "", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; private static final String[] chineseMonthNames = { "", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" }; private static final String[] chineseDateNames = { "", "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十" }; private static final String[] principleTermNames = { "大寒", "雨水", "春分", "谷雨", "夏滿", "夏至", "大暑", "處暑", "秋分", "霜降", "小雪", "冬至" }; private static final String[] sectionalTermNames = { "小寒", "立春", "驚蟄", "清明", "立夏", "芒種", "小暑", "立秋", "白露", "寒露", "立冬", "大雪" }; private static final String[] stemNames = { "", "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" }; private static final String[] branchNames = { "", "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" }; private static final String[] animalNames = { "", "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" }; /* 接下來(lái)是數(shù)據(jù)壓縮表~ */ private static final int[] bigLeapMonthYears = { 6, 14, 19, 25, 33, 36, 38, 41, 44, 52, 55, 79, 117, 136, 147, 150, 155, 158, 185, 193 }; private static final char[][] sectionalTermMap = { { 7, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5 }, { 5, 4, 5, 5, 5, 4, 4, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 3, 3, 4, 4, 3, 3, 3 }, { 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 5 }, { 5, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 4, 4, 5, 5, 4, 4, 4, 5, 4, 4, 4, 4, 5 }, { 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 5 }, { 6, 6, 7, 7, 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 5 }, { 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6, 7, 7, 6, 6, 6, 7, 7 }, { 8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6, 7, 7, 7 }, { 8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 7 }, { 9, 9, 9, 9, 8, 9, 9, 9, 8, 8, 9, 9, 8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 8 }, { 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6, 7, 7, 7 }, { 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6, 7, 7, 6, 6, 6, 7, 7 } }; private static final char[][] sectionalTermYear = { { 13, 49, 85, 117, 149, 185, 201, 250, 250 }, { 13, 45, 81, 117, 149, 185, 201, 250, 250 }, { 13, 48, 84, 112, 148, 184, 200, 201, 250 }, { 13, 45, 76, 108, 140, 172, 200, 201, 250 }, { 13, 44, 72, 104, 132, 168, 200, 201, 250 }, { 5, 33, 68, 96, 124, 152, 188, 200, 201 }, { 29, 57, 85, 120, 148, 176, 200, 201, 250 }, { 13, 48, 76, 104, 132, 168, 196, 200, 201 }, { 25, 60, 88, 120, 148, 184, 200, 201, 250 }, { 16, 44, 76, 108, 144, 172, 200, 201, 250 }, { 28, 60, 92, 124, 160, 192, 200, 201, 250 }, { 17, 53, 85, 124, 156, 188, 200, 201, 250 } }; private static final char[][] principleTermMap = { { 21, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 20, 20, 20, 20, 20, 19, 20, 20, 20, 19, 19, 20 }, { 20, 19, 19, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19, 18, 19, 19, 19, 18, 18, 19, 19, 18, 18, 18, 18, 18, 18, 18 }, { 21, 21, 21, 22, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 20, 20, 20, 20, 19, 20, 20, 20, 20 }, { 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 20, 20, 20, 20, 19, 20, 20, 20, 19, 19, 20, 20, 19, 19, 19, 20, 20 }, { 21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 21 }, { 22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 21 }, { 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 23 }, { 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 23 }, { 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 23 }, { 24, 24, 24, 24, 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 23 }, { 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 22 }, { 22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21, 22 } }; private static final char[][] principleTermYear = { { 13, 45, 81, 113, 149, 185, 201 }, { 21, 57, 93, 125, 161, 193, 201 }, { 21, 56, 88, 120, 152, 188, 200, 201 }, { 21, 49, 81, 116, 144, 176, 200, 201 }, { 17, 49, 77, 112, 140, 168, 200, 201 }, { 28, 60, 88, 116, 148, 180, 200, 201 }, { 25, 53, 84, 112, 144, 172, 200, 201 }, { 29, 57, 89, 120, 148, 180, 200, 201 }, { 17, 45, 73, 108, 140, 168, 200, 201 }, { 28, 60, 92, 124, 160, 192, 200, 201 }, { 16, 44, 80, 112, 148, 180, 200, 201 }, { 17, 53, 88, 120, 156, 188, 200, 201 } }; private static final char[] daysInGregorianMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; private static final char[] chineseMonths = { 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 }; private String getChineseTerm() { if (get(Calendar.DATE) == get(CHINESE_SECTIONAL_TERM)) { return sectionalTermNames[get(Calendar.MONTH)]; } else if (get(Calendar.DATE) == get(CHINESE_PRINCIPLE_TERM)) { return principleTermNames[get(Calendar.MONTH)]; } else return null; } // add by skywang private String getLunarFestival() { int day = get(CHINESE_DATE); int month = get(CHINESE_MONTH); String sToday = day < 10 ? "0" + day:"" + day; String sMonth = month<10 ? "0" +(month):""+(month); return lFestival.get(sMonth+sToday); } private String getSolarFestival() { int day = get(Calendar.DATE); int month = get(Calendar.MONTH); String sToday = day < 10 ? "0" + day:"" + day; String sMonth = month<10 ? "0" +(month+1):""+(month+1); return sFestival.get(sMonth+sToday); } private String getFestivalOrTermOrDate() { String ret; if ((ret = getSolarFestival()) != null) return ret; if ((ret = getLunarFestival()) != null) return ret; return getChinese(get(CHINESE_TERM_OR_DATE)); } //公歷節(jié)日 private static HashMap<String,String> sFestival =new HashMap<String,String>(); // 農(nóng)歷介入 private static HashMap<String,String> lFestival =new HashMap<String,String>(); static { sFestival.put("0101","元旦"); sFestival.put("0214","情人節(jié)"); sFestival.put("0308","婦女節(jié)"); sFestival.put("0312","植樹(shù)節(jié)"); sFestival.put("0401","愚人節(jié)"); sFestival.put("0501","勞動(dòng)節(jié)"); sFestival.put("0504","青年節(jié)"); sFestival.put("0601","兒童節(jié)"); sFestival.put("0701","建黨節(jié)"); sFestival.put("0801","建軍節(jié)"); sFestival.put("0910","教師節(jié)"); sFestival.put("1001","國(guó)慶節(jié)"); sFestival.put("1031","萬(wàn)圣節(jié)"); // sFestival.put("1112",""); sFestival.put("1225","圣誕節(jié)"); lFestival.put("0101","春節(jié)"); // lFestival.put("0102","大年初二"); // lFestival.put("0103","大年初三"); lFestival.put("0115","元宵節(jié)"); lFestival.put("0505","端午節(jié)"); lFestival.put("0707","七夕"); lFestival.put("0815","中秋節(jié)"); lFestival.put("0909","重陽(yáng)節(jié)"); lFestival.put("1208","臘八節(jié)"); // lFestival.put("1299","除夕"); } }
相關(guān)文章
JAVA格式化時(shí)間日期的簡(jiǎn)單實(shí)例
這篇文章主要介紹了JAVA格式化時(shí)間日期的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-11-11java調(diào)用shell腳本及注意事項(xiàng)說(shuō)明
這篇文章主要介紹了java調(diào)用shell腳本及注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06SpringMvc+Angularjs 實(shí)現(xiàn)多文件批量上傳
本文通過(guò)實(shí)例代碼給大家講解了SpringMvc+Angularjs 實(shí)現(xiàn)多文件批量上傳功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友一起學(xué)習(xí)吧2017-03-03Mybatis下動(dòng)態(tài)sql中##和$$的區(qū)別講解
今天小編就為大家分享一篇關(guān)于Mybatis下動(dòng)態(tài)sql中##和$$的區(qū)別講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03Struts1和struts2的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Struts1和struts2的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09解決JD-GUI for mac big sur打不開(kāi)問(wèn)題
這篇文章主要介紹了解決JD-GUI for mac big sur打不開(kāi)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Spring?BeanDefinition收集過(guò)程示例詳解
這篇文章主要為大家介紹了Spring?BeanDefinition收集過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Java 實(shí)現(xiàn)萬(wàn)年歷總結(jié)
這篇文章主要介紹了Java 萬(wàn)年歷實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09Spring?Boot?使用?SSE?方式向前端推送數(shù)據(jù)詳解
這篇文章主要介紹了Spring?Boot?使用SSE方式向前端推送數(shù)據(jù)詳解,SSE簡(jiǎn)單的來(lái)說(shuō)就是服務(wù)器主動(dòng)向前端推送數(shù)據(jù)的一種技術(shù),它是單向的,也就是說(shuō)前端是不能向服務(wù)器發(fā)送數(shù)據(jù)的2022-08-08java使用單向鏈表解決數(shù)據(jù)存儲(chǔ)自定義排序問(wèn)題
本文主要介紹了java使用單向鏈表解決數(shù)據(jù)存儲(chǔ)自定義排序問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03