农历、节气、节日算法

[java]  view plain  copy
  1. /** 
  2.  * @(#)Lunar.java 2008-4-5 2:06:22 
  3.  */  
  4. package luozhuangChineseEra;  
  5.   
  6. import java.text.SimpleDateFormat;  
  7. import java.util.ArrayList;  
  8. import java.util.Arrays;  
  9. import java.util.Calendar;  
  10. import java.util.Date;  
  11. import java.util.GregorianCalendar;  
  12. import java.util.List;  
  13. import java.util.TimeZone;  
  14. import java.util.Vector;  
  15. import java.util.regex.Matcher;  
  16. import java.util.regex.Pattern;  
  17.   
  18. /** 
  19.  * 提供一些农历相关信息 
  20.  * 
  21.  * @author 林志斌,修改by 大师♂罗莊 
  22.  */  
  23. public class Lunar {  
  24.   
  25.     public static void main(String[] args) {  
  26.         Lunar l = new Lunar(System.currentTimeMillis());  
  27.         System.out.println("节气:" + l.getTermString());  
  28.         System.out.println("干支历:" + l.getCyclicalDateString());  
  29.         System.out.println("星期" + l.getDayOfWeek());  
  30.         System.out.println("农历" + l.getLunarDateString());  
  31.         Calendar t = Calendar.getInstance();  
  32.         t.set(19851017);  
  33.         System.out.print(t.getTime().getMonth() + "月");  
  34.         System.out.println(getbeforesolarTerm(t.get(Calendar.YEAR), t.getTime()));  
  35.         System.out.println(getaftersolarTerm(t.get(Calendar.YEAR), t.getTime()));  
  36.   
  37.   
  38.         Date[] jieqi = jieqilist(1940);  
  39.         for (int i = 0; i < solarTerm.length; i++) {  
  40.             System.out.print(solarTerm[i]);  
  41.             System.out.print(jieqi[i].getMonth() + "月");  
  42.             System.out.println(jieqi[i].getDate());  
  43.         }  
  44.     }  
  45.   
  46.     /** 
  47.      * 获得某天前个节气日期差 
  48.      * 
  49.      * @return 日期数 
  50.      */  
  51.     public static long getbeforesolarTerm(int year, Date date) {  
  52.        List<Date> jieqi =Alljieqi(year);  
  53.         int[] jieqibeforeafter = getnearsolarTerm(year, date);  
  54.         return MyDate.DateDays(date, jieqi.get(jieqibeforeafter[0]));  
  55.   
  56.   
  57.     }  
  58.   
  59.     /** 
  60.      * 获得某天后个节气日期差 
  61.      * 
  62.      * @return 日期数 
  63.      */  
  64.     public static long getaftersolarTerm(int year, Date date) {  
  65.         List<Date> jieqi =Alljieqi(year);  
  66.         int[] jieqibeforeafter = getnearsolarTerm(year, date);  
  67.         return MyDate.DateDays(date, jieqi.get(jieqibeforeafter[1]));  
  68.   
  69.     }  
  70.       
  71.       
  72.     public static List<Date> Alljieqi(int year)  
  73.     {  
  74.       
  75.       List<Date> jieqi = new  ArrayList<Date>();  
  76.         Date[] temp ;  
  77.         temp = jieqilist(year - 1);  
  78.         jieqi.addAll(Arrays.asList(temp));  
  79.         temp = jieqilist(year);  
  80.         jieqi.addAll(Arrays.asList(temp));  
  81.          temp = jieqilist(year+1);  
  82.         jieqi.addAll(Arrays.asList(temp));  
  83.         return jieqi;  
  84.     }  
  85.   
  86.     /** 
  87.      * 获得某天前后两个节气序号 
  88.      * 
  89.      * @return 
  90.      */  
  91.     public static int[] getnearsolarTerm(int year, Date date) {  
  92.       List<Date> jieqi =Alljieqi(year);  
  93.           
  94.         int[] returnValue = new int[2];  
  95.         for (int i = 0; i < jieqi.size(); i++) {  
  96.             if (date.getTime() > jieqi.get(i).getTime()) {  
  97.                 continue;  
  98.             }  
  99.             if (i % 2 == 0) {//只管气  
  100.                 returnValue[0] = i - 2;  
  101.                 returnValue[1] = i;  
  102.             } else {  
  103.                 returnValue[0] = i - 1;  
  104.                 returnValue[1] = i + 1;  
  105.   
  106.             }  
  107.             break;  
  108.         }  
  109.   
  110.         return returnValue;  
  111.     }  
  112.   
  113.     /** 
  114.      * 获得某年中所有节气Date 
  115.      * 
  116.      * @return 
  117.      */  
  118.     public static Date[] jieqilist(int year) {  
  119.         Date[] returnvalue = new Date[solarTerm.length];  
  120.   
  121.         for (int i = 0; i < solarTerm.length; i++) {  
  122.   
  123.             Date t = getSolarTermCalendar(year, i);  
  124.             returnvalue[i] = t;  
  125.   
  126.         }  
  127.         return returnvalue;  
  128.     }  
  129.     private final static int[] lunarInfo = {  
  130.         0x4bd80x4ae00xa5700x54d50xd2600xd9500x55540x56af,  
  131.         0x9ad00x55d20x4ae00xa5b60xa4d00xd2500xd2950xb54f,  
  132.         0xd6a00xada20x95b00x49770x497f0xa4b00xb4b50x6a50,  
  133.         0x6d400xab540x2b6f0x95700x52f20x49700x65660xd4a0,  
  134.         0xea500x6a950x5adf0x2b600x86e30x92ef0xc8d70xc95f,  
  135.         0xd4a00xd8a60xb55f0x56a00xa5b40x25df0x92d00xd2b2,  
  136.         0xa9500xb5570x6ca00xb5500x53550x4daf0xa5b00x4573,  
  137.         0x52bf0xa9a80xe9500x6aa00xaea60xab500x4b600xaae4,  
  138.         0xa5700x52600xf2630xd9500x5b570x56a00x96d00x4dd5,  
  139.         0x4ad00xa4d00xd4d40xd2500xd5580xb5400xb6a00x95a6,  
  140.         0x95bf0x49b00xa9740xa4b00xb27a0x6a500x6d400xaf46,  
  141.         0xab600x95700x4af50x49700x64b00x74a30xea500x6b58,  
  142.         0x5ac00xab600x96d50x92e00xc9600xd9540xd4a00xda50,  
  143.         0x75520x56a00xabb70x25d00x92d00xcab50xa9500xb4a0,  
  144.         0xbaa40xad500x55d90x4ba00xa5b00x51760x52bf0xa930,  
  145.         0x79540x6aa00xad500x5b520x4b600xa6e60xa4e00xd260,  
  146.         0xea650xd5300x5aa00x76a30x96d00x4afb0x4ad00xa4d0,  
  147.         0xd0b60xd25f0xd5200xdd450xb5a00x56d00x55b20x49b0,  
  148.         0xa5770xa4b00xaa500xb2550x6d2f0xada00x4b630x937f,  
  149.         0x49f80x49700x64b00x68a60xea5f0x6b200xa6c40xaaef,  
  150.         0x92e00xd2e30xc9600xd5570xd4a00xda500x5d550x56a0,  
  151.         0xa6d00x55d40x52d00xa9b80xa9500xb4a00xb6a60xad50,  
  152.         0x55a00xaba40xa5b00x52b00xb2730x69300x73370x6aa0,  
  153.         0xad500x4b550x4b6f0xa5700x54e40xd2600xe9680xd520,  
  154.         0xdaa00x6aa60x56df0x4ae00xa9d40xa4d00xd1500xf2520xd520  
  155.     };  
  156.     private final static int[] solarTermInfo = {  
  157.         021208424676383685337107014128867150921,  
  158.         173149195551218072240693263343285989308563331033,  
  159.         353350375494397447419210440795462224483532504758  
  160.     };  
  161.     public final static String[] Tianan = {  
  162.         "甲""乙""丙""丁""戊""己""庚""辛""壬""癸"  
  163.     };  
  164.     public final static String[] Deqi = {  
  165.         "子""丑""寅""卯""辰""巳""午""未""申""酉""戌""亥"  
  166.     };  
  167.     public final static String[] Animals = {  
  168.         "鼠""牛""虎""兔""龙""蛇""马""羊""猴""鸡""狗""猪"  
  169.     };  
  170.     public final static String[] solarTerm = {  
  171.         "小寒""大寒""立春""雨水""惊蛰""春分",  
  172.         "清明""谷雨""立夏""小满""芒种""夏至",  
  173.         "小暑""大暑""立秋""处暑""白露""秋分",  
  174.         "寒露""霜降""立冬""小雪""大雪""冬至"  
  175.     };  
  176.     public final static String[] lunarString1 = {  
  177.         "零""一""二""三""四""五""六""七""八""九"  
  178.     };  
  179.     public final static String[] lunarString2 = {  
  180.         "初""十""廿""卅""正""腊""冬""闰"  
  181.     };  
  182.     /** 
  183.      * 国历节日 *表示放假日 
  184.      */  
  185.     private final static String[] sFtv = {  
  186.         "0101*元旦""0214 情人节""0308 妇女节""0312 植树节",  
  187.         "0315 消费者权益日""0401 愚人节""0501*劳动节""0504 青年节",  
  188.         "0509 郝维节""0512 护士节""0601 儿童节""0701 建党节 香港回归纪念",  
  189.         "0801 建军节""0808 父亲节""0816 燕衔泥节""0909 毛泽东逝世纪念",  
  190.         "0910 教师节""0928 孔子诞辰""1001*国庆节""1006 老人节",  
  191.         "1024 联合国日""1111 光棍节""1112 孙中山诞辰纪念""1220 澳门回归纪念",  
  192.         "1225 圣诞节""1226 毛泽东诞辰纪念"  
  193.     };  
  194.     /** 
  195.      * 农历节日 *表示放假日 
  196.      */  
  197.     private final static String[] lFtv = {  
  198.         "0101*春节、弥勒佛诞""0106 定光佛诞""0115 元宵节",  
  199.         "0208 释迦牟尼佛出家""0215 释迦牟尼佛涅槃""0209 海空上师诞",  
  200.         "0219 观世音菩萨诞""0221 普贤菩萨诞""0316 准提菩萨诞",  
  201.         "0404 文殊菩萨诞""0408 释迦牟尼佛诞""0415 佛吉祥日——释迦牟尼佛诞生、成道、涅槃三期同一庆(即南传佛教国家的卫塞节)",  
  202.         "0505 端午节""0513 伽蓝菩萨诞""0603 护法韦驮尊天菩萨诞",  
  203.         "0619 观世音菩萨成道——此日放生、念佛,功德殊胜",  
  204.         "0707 七夕情人节""0713 大势至菩萨诞""0715 中元节",  
  205.         "0724 龙树菩萨诞""0730 地藏菩萨诞""0815 中秋节",  
  206.         "0822 燃灯佛诞""0909 重阳节""0919 观世音菩萨出家纪念日",  
  207.         "0930 药师琉璃光如来诞""1005 达摩祖师诞""1107 阿弥陀佛诞",  
  208.         "1208 释迦如来成道日,腊八节""1224 小年",  
  209.         "1229 华严菩萨诞""0100*除夕"  
  210.     };  
  211.     /** 
  212.      * 某月的第几个星期几 
  213.      */  
  214.     private static String[] wFtv = {  
  215.         "0520 母亲节""0716 合作节""0730 被奴役国家周"  
  216.     };  
  217.   
  218.     private static int toInt(String str) {  
  219.         try {  
  220.             return Integer.parseInt(str);  
  221.         } catch (Exception e) {  
  222.             return -1;  
  223.         }  
  224.     }  
  225.     private final static Pattern sFreg = Pattern.compile("^(\\d{2})(\\d{2})([\\s\\*])(.+)$");  
  226.     private final static Pattern wFreg = Pattern.compile("^(\\d{2})(\\d)(\\d)([\\s\\*])(.+)$");  
  227.   
  228.     private synchronized void findFestival() {  
  229.         int sM = this.getSolarMonth();  
  230.         int sD = this.getSolarDay();  
  231.         int lM = this.getLunarMonth();  
  232.         int lD = this.getLunarDay();  
  233.         int sy = this.getSolarYear();  
  234.         Matcher m;  
  235.         for (int i = 0; i < Lunar.sFtv.length; i++) {  
  236.             m = Lunar.sFreg.matcher(Lunar.sFtv[i]);  
  237.             if (m.find()) {  
  238.                 if (sM == Lunar.toInt(m.group(1)) && sD == Lunar.toInt(m.group(2))) {  
  239.                     this.isSFestival = true;  
  240.                     this.sFestivalName = m.group(4);  
  241.                     if ("*".equals(m.group(3))) {  
  242.                         this.isHoliday = true;  
  243.                     }  
  244.                     break;  
  245.                 }  
  246.             }  
  247.         }  
  248.         for (int i = 0; i < Lunar.lFtv.length; i++) {  
  249.             m = Lunar.sFreg.matcher(Lunar.lFtv[i]);  
  250.             if (m.find()) {  
  251.                 if (lM == Lunar.toInt(m.group(1)) && lD == Lunar.toInt(m.group(2))) {  
  252.                     this.isLFestival = true;  
  253.                     this.lFestivalName = m.group(4);  
  254.                     if ("*".equals(m.group(3))) {  
  255.                         this.isHoliday = true;  
  256.                     }  
  257.                     break;  
  258.                 }  
  259.             }  
  260.         }  
  261.   
  262.         // 月周节日     
  263.         int w, d;  
  264.         for (int i = 0; i < Lunar.wFtv.length; i++) {  
  265.             m = Lunar.wFreg.matcher(Lunar.wFtv[i]);  
  266.             if (m.find()) {  
  267.                 if (this.getSolarMonth() == Lunar.toInt(m.group(1))) {  
  268.                     w = Lunar.toInt(m.group(2));  
  269.                     d = Lunar.toInt(m.group(3));  
  270.                     if (this.solar.get(Calendar.WEEK_OF_MONTH) == w  
  271.                             && this.solar.get(Calendar.DAY_OF_WEEK) == d) {  
  272.                         this.isSFestival = true;  
  273.                         this.sFestivalName += "|" + m.group(5);  
  274.                         if ("*".equals(m.group(4))) {  
  275.                             this.isHoliday = true;  
  276.                         }  
  277.                     }  
  278.                 }  
  279.             }  
  280.         }  
  281.         if (sy > 1874 && sy < 1909) {  
  282.             this.description = "光绪" + (((sy - 1874) == 1) ? "元" : "" + (sy - 1874));  
  283.         }  
  284.         if (sy > 1908 && sy < 1912) {  
  285.             this.description = "宣统" + (((sy - 1908) == 1) ? "元" : String.valueOf(sy - 1908));  
  286.         }  
  287.         if (sy > 1911 && sy < 1950) {  
  288.             this.description = "民国" + (((sy - 1911) == 1) ? "元" : String.valueOf(sy - 1911));  
  289.         }  
  290.         if (sy > 1949) {  
  291.             this.description = "共和国" + (((sy - 1949) == 1) ? "元" : String.valueOf(sy - 1949));  
  292.         }  
  293.         this.description += "年";  
  294.         this.sFestivalName = this.sFestivalName.replaceFirst("^\\|""");  
  295.         this.isFinded = true;  
  296.     }  
  297.     private boolean isFinded = false;  
  298.     private boolean isSFestival = false;  
  299.     private boolean isLFestival = false;  
  300.     private String sFestivalName = "";  
  301.     private String lFestivalName = "";  
  302.     private String description = "";  
  303.     private boolean isHoliday = false;  
  304.   
  305.     /** 
  306.      * 返回农历年闰月月份 
  307.      * 
  308.      * @param lunarYear 指定农历年份(数字) 
  309.      * @return 该农历年闰月的月份(数字,没闰返回0) 
  310.      */  
  311.     private static int getLunarLeapMonth(int lunarYear) {  
  312.         // 数据表中,每个农历年用16bit来表示,     
  313.         // 前12bit分别表示12个月份的大小月,最后4bit表示闰月     
  314.         // 若4bit全为1或全为0,表示没闰, 否则4bit的值为闰月月份     
  315.         int leapMonth = Lunar.lunarInfo[lunarYear - 1900] & 0xf;  
  316.         leapMonth = (leapMonth == 0xf ? 0 : leapMonth);  
  317.         return leapMonth;  
  318.     }  
  319.   
  320.     /** 
  321.      * 返回农历年闰月的天数 
  322.      * 
  323.      * @param lunarYear 指定农历年份(数字) 
  324.      * @return 该农历年闰月的天数(数字) 
  325.      */  
  326.     private static int getLunarLeapDays(int lunarYear) {  
  327.         // 下一年最后4bit为1111,返回30(大月)     
  328.         // 下一年最后4bit不为1111,返回29(小月)     
  329.         // 若该年没有闰月,返回0     
  330.         return Lunar.getLunarLeapMonth(lunarYear) > 0 ? ((Lunar.lunarInfo[lunarYear - 1899] & 0xf) == 0xf ? 30  
  331.                 : 29)  
  332.                 : 0;  
  333.     }  
  334.   
  335.     /** 
  336.      * 返回农历年的总天数 
  337.      * 
  338.      * @param lunarYear 指定农历年份(数字) 
  339.      * @return 该农历年的总天数(数字) 
  340.      */  
  341.     private static int getLunarYearDays(int lunarYear) {  
  342.         // 按小月计算,农历年最少有12 * 29 = 348天     
  343.         int daysInLunarYear = 348;  
  344.         // 数据表中,每个农历年用16bit来表示,     
  345.         // 前12bit分别表示12个月份的大小月,最后4bit表示闰月     
  346.         // 每个大月累加一天     
  347.         for (int i = 0x8000; i > 0x8; i >>= 1) {  
  348.             daysInLunarYear += ((Lunar.lunarInfo[lunarYear - 1900] & i) != 0) ? 1  
  349.                     : 0;  
  350.         }  
  351.         // 加上闰月天数     
  352.         daysInLunarYear += Lunar.getLunarLeapDays(lunarYear);  
  353.   
  354.         return daysInLunarYear;  
  355.     }  
  356.   
  357.     /** 
  358.      * 返回农历年正常月份的总天数 
  359.      * 
  360.      * @param lunarYear 指定农历年份(数字) 
  361.      * @param lunarMonth 指定农历月份(数字) 
  362.      * @return 该农历年闰月的月份(数字,没闰返回0) 
  363.      */  
  364.     private static int getLunarMonthDays(int lunarYear, int lunarMonth) {  
  365.         // 数据表中,每个农历年用16bit来表示,     
  366.         // 前12bit分别表示12个月份的大小月,最后4bit表示闰月     
  367.         int daysInLunarMonth = ((Lunar.lunarInfo[lunarYear - 1900] & (0x10000 >> lunarMonth)) != 0) ? 30  
  368.                 : 29;  
  369.         return daysInLunarMonth;  
  370.     }  
  371.   
  372.     /** 
  373.      * 取 Date 对象中用全球标准时间 (UTC) 表示的日期 
  374.      * 
  375.      * @param date 指定日期 
  376.      * @return UTC 全球标准时间 (UTC) 表示的日期 
  377.      */  
  378.     public static synchronized int getUTCDay(Date date) {  
  379.         Lunar.makeUTCCalendar();  
  380.         synchronized (utcCal) {  
  381.             utcCal.clear();  
  382.             utcCal.setTimeInMillis(date.getTime());  
  383.             return utcCal.get(Calendar.DAY_OF_MONTH);  
  384.         }  
  385.     }  
  386.     private static GregorianCalendar utcCal = null;  
  387.   
  388.     private static synchronized void makeUTCCalendar() {  
  389.         if (Lunar.utcCal == null) {  
  390.             Lunar.utcCal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));  
  391.         }  
  392.     }  
  393.   
  394.     /** 
  395.      * 返回全球标准时间 (UTC) (或 GMT) 的 1970 年 1 月 1 日到所指定日期之间所间隔的毫秒数。 
  396.      * 
  397.      * @param y 指定年份 
  398.      * @param m 指定月份 
  399.      * @param d 指定日期 
  400.      * @param h 指定小时 
  401.      * @param min 指定分钟 
  402.      * @param sec 指定秒数 
  403.      * @return 全球标准时间 (UTC) (或 GMT) 的 1970 年 1 月 1 日到所指定日期之间所间隔的毫秒数 
  404.      */  
  405.     public static synchronized long UTC(int y, int m, int d, int h, int min, int sec) {  
  406.         Lunar.makeUTCCalendar();  
  407.         synchronized (utcCal) {  
  408.             utcCal.clear();  
  409.             utcCal.set(y, m, d, h, min, sec);  
  410.             return utcCal.getTimeInMillis();  
  411.         }  
  412.     }  
  413.   
  414.     /** 
  415.      * 返回公历年节气的日期 
  416.      * 
  417.      * @param solarYear 指定公历年份(数字) 
  418.      * @param index 指定节气序号(数字,0从小寒算起) 
  419.      * @return 日期(数字,所在月份的第几天) 
  420.      */  
  421.     private static int getSolarTermDay(int solarYear, int index) {  
  422.   
  423.         return Lunar.getUTCDay(getSolarTermCalendar(solarYear, index));  
  424.     }  
  425.   
  426.     /** 
  427.      * 返回公历年节气的日期 
  428.      * 
  429.      * @param solarYear 指定公历年份(数字) 
  430.      * @param index 指定节气序号(数字,0从小寒算起) 
  431.      * @return 日期(数字,所在月份的第几天) 
  432.      */  
  433.     public static Date getSolarTermCalendar(int solarYear, int index) {  
  434.         long l = (long31556925974.7 * (solarYear - 1900)  
  435.                 + solarTermInfo[index] * 60000L;  
  436.         l = l + Lunar.UTC(190006250);  
  437.         return new Date(l);  
  438.     }  
  439.     private Calendar solar;  
  440.     private int lunarYear;  
  441.     private int lunarMonth;  
  442.     private int lunarDay;  
  443.     private boolean isLeap;  
  444.     private boolean isLeapYear;  
  445.     private int solarYear;  
  446.     private int solarMonth;  
  447.     private int solarDay;  
  448.     private int cyclicalYear = 0;  
  449.     private int cyclicalMonth = 0;  
  450.     private int cyclicalDay = 0;  
  451.     private int maxDayInMonth = 29;  
  452.   
  453.     /** 
  454.      * 通过 Date 对象构建农历信息 
  455.      * 
  456.      * @param date 指定日期对象 
  457.      */  
  458.     public Lunar(Date date) {  
  459.         if (date == null) {  
  460.             date = new Date();  
  461.         }  
  462.         this.init(date.getTime());  
  463.     }  
  464.   
  465.     /** 
  466.      * 通过 TimeInMillis 构建农历信息 
  467.      * 
  468.      * @param TimeInMillis 
  469.      */  
  470.     public Lunar(long TimeInMillis) {  
  471.         this.init(TimeInMillis);  
  472.     }  
  473.   
  474.     private void init(long TimeInMillis) {  
  475.         this.solar = Calendar.getInstance();  
  476.         this.solar.setTimeInMillis(TimeInMillis);  
  477.         Calendar baseDate = new GregorianCalendar(1900031);  
  478.         long offset = (TimeInMillis - baseDate.getTimeInMillis()) / 86400000;  
  479.         // 按农历年递减每年的农历天数,确定农历年份     
  480.         this.lunarYear = 1900;  
  481.         int daysInLunarYear = Lunar.getLunarYearDays(this.lunarYear);  
  482.         while (this.lunarYear < 2100 && offset >= daysInLunarYear) {  
  483.             offset -= daysInLunarYear;  
  484.             daysInLunarYear = Lunar.getLunarYearDays(++this.lunarYear);  
  485.         }  
  486.         // 农历年数字     
  487.   
  488.         // 按农历月递减每月的农历天数,确定农历月份     
  489.         int lunarMonth = 1;  
  490.         // 所在农历年闰哪个月,若没有返回0     
  491.         int leapMonth = Lunar.getLunarLeapMonth(this.lunarYear);  
  492.         // 是否闰年     
  493.         this.isLeapYear = leapMonth > 0;  
  494.         // 闰月是否递减     
  495.         boolean leapDec = false;  
  496.         boolean isLeap = false;  
  497.         int daysInLunarMonth = 0;  
  498.         while (lunarMonth < 13 && offset > 0) {  
  499.             if (isLeap && leapDec) { // 如果是闰年,并且是闰月     
  500.                 // 所在农历年闰月的天数     
  501.                 daysInLunarMonth = Lunar.getLunarLeapDays(this.lunarYear);  
  502.                 leapDec = false;  
  503.             } else {  
  504.                 // 所在农历年指定月的天数     
  505.                 daysInLunarMonth = Lunar.getLunarMonthDays(this.lunarYear, lunarMonth);  
  506.             }  
  507.             if (offset < daysInLunarMonth) {  
  508.                 break;  
  509.             }  
  510.             offset -= daysInLunarMonth;  
  511.   
  512.             if (leapMonth == lunarMonth && isLeap == false) {  
  513.                 // 下个月是闰月     
  514.                 leapDec = true;  
  515.                 isLeap = true;  
  516.             } else {  
  517.                 // 月份递增     
  518.                 lunarMonth++;  
  519.             }  
  520.         }  
  521.         this.maxDayInMonth = daysInLunarMonth;  
  522.         // 农历月数字     
  523.         this.lunarMonth = lunarMonth;  
  524.         // 是否闰月     
  525.         this.isLeap = (lunarMonth == leapMonth && isLeap);  
  526.         // 农历日数字     
  527.         this.lunarDay = (int) offset + 1;  
  528.         // 取得干支历     
  529.         this.getCyclicalData();  
  530.     }  
  531.   
  532.     /** 
  533.      * 取干支历 不是历年,历月干支,而是中国的从立春节气开始的节月,是中国的太阳十二宫,阳历的。 
  534.      * 
  535.      * @param cncaData 日历对象(Tcnca) 
  536.      */  
  537.     private void getCyclicalData() {  
  538.         this.solarYear = this.solar.get(Calendar.YEAR);  
  539.         this.solarMonth = this.solar.get(Calendar.MONTH);  
  540.         this.solarDay = this.solar.get(Calendar.DAY_OF_MONTH);  
  541.         // 干支历     
  542.         int cyclicalYear = 0;  
  543.         int cyclicalMonth = 0;  
  544.         int cyclicalDay = 0;  
  545.   
  546.         // 干支年 1900年立春後为庚子年(60进制36)     
  547.         int term2 = Lunar.getSolarTermDay(solarYear, 2); // 立春日期     
  548.         // 依节气调整二月分的年柱, 以立春为界     
  549.         if (solarMonth < 1 || (solarMonth == 1 && solarDay < term2)) {  
  550.             cyclicalYear = (solarYear - 1900 + 36 - 1) % 60;  
  551.         } else {  
  552.             cyclicalYear = (solarYear - 1900 + 36) % 60;  
  553.         }  
  554.   
  555.         // 干支月 1900年1月小寒以前为 丙子月(60进制12)     
  556.         int firstNode = Lunar.getSolarTermDay(solarYear, solarMonth * 2); // 传回当月「节」为几日开始     
  557.         // 依节气月柱, 以「节」为界     
  558.         if (solarDay < firstNode) {  
  559.             cyclicalMonth = ((solarYear - 1900) * 12 + solarMonth + 12) % 60;  
  560.         } else {  
  561.             cyclicalMonth = ((solarYear - 1900) * 12 + solarMonth + 13) % 60;  
  562.         }  
  563.   
  564.         // 当月一日与 1900/1/1 相差天数     
  565.         // 1900/1/1与 1970/1/1 相差25567日, 1900/1/1 日柱为甲戌日(60进制10)     
  566.         cyclicalDay = (int) (Lunar.UTC(solarYear, solarMonth, solarDay, 000) / 86400000 + 25567 + 10) % 60;  
  567.         this.cyclicalYear = cyclicalYear;  
  568.         this.cyclicalMonth = cyclicalMonth;  
  569.         this.cyclicalDay = cyclicalDay;  
  570.     }  
  571.   
  572.     /** 
  573.      * 取农历年生肖 
  574.      * 
  575.      * @return 农历年生肖(例:龙) 
  576.      */  
  577.     public String getAnimalString() {  
  578.         return Lunar.Animals[(this.lunarYear - 4) % 12];  
  579.     }  
  580.   
  581.     /** 
  582.      * 返回公历日期的节气字符串 
  583.      * 
  584.      * @return 二十四节气字符串,若不是节气日,返回空串(例:冬至) 
  585.      */  
  586.     public String getTermString() {  
  587.         // 二十四节气     
  588.         String termString = "";  
  589.         if (Lunar.getSolarTermDay(solarYear, solarMonth * 2) == solarDay) {  
  590.             termString = Lunar.solarTerm[solarMonth * 2];  
  591.         } else if (Lunar.getSolarTermDay(solarYear, solarMonth * 2 + 1) == solarDay) {  
  592.             termString = Lunar.solarTerm[solarMonth * 2 + 1];  
  593.         }  
  594.         return termString;  
  595.     }  
  596.   
  597.     /** 
  598.      * 取得干支历字符串 
  599.      * 
  600.      * @return 干支历字符串(例:甲子年甲子月甲子日) 
  601.      */  
  602.     public String getCyclicalDateString() {  
  603.         return this.getCyclicaYear() + "年" + this.getCyclicaMonth() + "月"  
  604.                 + this.getCyclicaDay() + "日";  
  605.     }  
  606.   
  607.     /** 
  608.      * 年份天干 
  609.      * 
  610.      * @return 年份天干 
  611.      */  
  612.     public int getTiananY() {  
  613.         return Lunar.getTianan(this.cyclicalYear);  
  614.     }  
  615.   
  616.     /** 
  617.      * 月份天干 
  618.      * 
  619.      * @return 月份天干 
  620.      */  
  621.     public int getTiananM() {  
  622.         return Lunar.getTianan(this.cyclicalMonth);  
  623.     }  
  624.   
  625.     /** 
  626.      * 日期天干 
  627.      * 
  628.      * @return 日期天干 
  629.      */  
  630.     public int getTiananD() {  
  631.         return Lunar.getTianan(this.cyclicalDay);  
  632.     }  
  633.   
  634.     /** 
  635.      * 年份地支 
  636.      * 
  637.      * @return 年分地支 
  638.      */  
  639.     public int getDeqiY() {  
  640.         return Lunar.getDeqi(this.cyclicalYear);  
  641.     }  
  642.   
  643.     /** 
  644.      * 月份地支 
  645.      * 
  646.      * @return 月份地支 
  647.      */  
  648.     public int getDeqiM() {  
  649.         return Lunar.getDeqi(this.cyclicalMonth);  
  650.     }  
  651.   
  652.     /** 
  653.      * 日期地支 
  654.      * 
  655.      * @return 日期地支 
  656.      */  
  657.     public int getDeqiD() {  
  658.         return Lunar.getDeqi(this.cyclicalDay);  
  659.     }  
  660.   
  661.     /** 
  662.      * 取得干支年字符串 
  663.      * 
  664.      * @return 干支年字符串 
  665.      */  
  666.     public String getCyclicaYear() {  
  667.         return Lunar.getCyclicalString(this.cyclicalYear);  
  668.     }  
  669.   
  670.     /** 
  671.      * 取得干支月字符串 
  672.      * 
  673.      * @return 干支月字符串 
  674.      */  
  675.     public String getCyclicaMonth() {  
  676.         return Lunar.getCyclicalString(this.cyclicalMonth);  
  677.     }  
  678.   
  679.     /** 
  680.      * 取得干支日字符串 
  681.      * 
  682.      * @return 干支日字符串 
  683.      */  
  684.     public String getCyclicaDay() {  
  685.         return Lunar.getCyclicalString(this.cyclicalDay);  
  686.     }  
  687.   
  688.     /** 
  689.      * 返回农历日期字符串 
  690.      * 
  691.      * @return 农历日期字符串 
  692.      */  
  693.     public String getLunarDayString() {  
  694.         return Lunar.getLunarDayString(this.lunarDay);  
  695.     }  
  696.   
  697.     /** 
  698.      * 返回农历日期字符串 
  699.      * 
  700.      * @return 农历日期字符串 
  701.      */  
  702.     public String getLunarMonthString() {  
  703.         return (this.isLeap() ? "闰" : "") + Lunar.getLunarMonthString(this.lunarMonth);  
  704.     }  
  705.   
  706.     /** 
  707.      * 返回农历日期字符串 
  708.      * 
  709.      * @return 农历日期字符串 
  710.      */  
  711.     public String getLunarYearString() {  
  712.         return Lunar.getLunarYearString(this.lunarYear);  
  713.     }  
  714.   
  715.     /** 
  716.      * 返回农历表示字符串 
  717.      * 
  718.      * @return 农历字符串(例:甲子年正月初三) 
  719.      */  
  720.     public String getLunarDateString() {  
  721.         return this.getLunarYearString() + "年"  
  722.                 + this.getLunarMonthString() + "月"  
  723.                 + this.getLunarDayString() + "日";  
  724.     }  
  725.   
  726.     /** 
  727.      * 农历年是否是闰月 
  728.      * 
  729.      * @return 农历年是否是闰月 
  730.      */  
  731.     public boolean isLeap() {  
  732.         return isLeap;  
  733.     }  
  734.   
  735.     /** 
  736.      * 农历年是否是闰年 
  737.      * 
  738.      * @return 农历年是否是闰年 
  739.      */  
  740.     public boolean isLeapYear() {  
  741.         return isLeapYear;  
  742.     }  
  743.   
  744.     /** 
  745.      * 当前农历月是否是大月 
  746.      * 
  747.      * @return 当前农历月是大月 
  748.      */  
  749.     public boolean isBigMonth() {  
  750.         return this.getMaxDayInMonth() > 29;  
  751.     }  
  752.   
  753.     /** 
  754.      * 当前农历月有多少天 
  755.      * 
  756.      * @return 当前农历月有多少天 
  757.      */  
  758.     public int getMaxDayInMonth() {  
  759.         return this.maxDayInMonth;  
  760.     }  
  761.   
  762.     /** 
  763.      * 农历日期 
  764.      * 
  765.      * @return 农历日期 
  766.      */  
  767.     public int getLunarDay() {  
  768.         return lunarDay;  
  769.     }  
  770.   
  771.     /** 
  772.      * 农历月份 
  773.      * 
  774.      * @return 农历月份 
  775.      */  
  776.     public int getLunarMonth() {  
  777.         return lunarMonth;  
  778.     }  
  779.   
  780.     /** 
  781.      * 农历年份 
  782.      * 
  783.      * @return 农历年份 
  784.      */  
  785.     public int getLunarYear() {  
  786.         return lunarYear;  
  787.     }  
  788.   
  789.     /** 
  790.      * 公历日期 
  791.      * 
  792.      * @return 公历日期 
  793.      */  
  794.     public int getSolarDay() {  
  795.         return solarDay;  
  796.     }  
  797.   
  798.     /** 
  799.      * 公历月份 
  800.      * 
  801.      * @return 公历月份 (不是从0算起) 
  802.      */  
  803.     public int getSolarMonth() {  
  804.         return solarMonth + 1;  
  805.     }  
  806.   
  807.     /** 
  808.      * 公历年份 
  809.      * 
  810.      * @return 公历年份 
  811.      */  
  812.     public int getSolarYear() {  
  813.         return solarYear;  
  814.     }  
  815.   
  816.     /** 
  817.      * 星期几 
  818.      * 
  819.      * @return 星期几(星期日为:1, 星期六为:7) 
  820.      */  
  821.     public int getDayOfWeek() {  
  822.         return this.solar.get(Calendar.DAY_OF_WEEK);  
  823.     }  
  824.   
  825.     /** 
  826.      * 黑色星期五 
  827.      * 
  828.      * @return 是否黑色星期五 
  829.      */  
  830.     public boolean isBlackFriday() {  
  831.         return (this.getSolarDay() == 13 && this.solar.get(Calendar.DAY_OF_WEEK) == 6);  
  832.     }  
  833.   
  834.     /** 
  835.      * 是否是今日 
  836.      * 
  837.      * @return 是否是今日 
  838.      */  
  839.     public boolean isToday() {  
  840.         Calendar clr = Calendar.getInstance();  
  841.         return clr.get(Calendar.YEAR) == this.solarYear  
  842.                 && clr.get(Calendar.MONTH) == this.solarMonth  
  843.                 && clr.get(Calendar.DAY_OF_MONTH) == this.solarDay;  
  844.     }  
  845.   
  846.     /** 
  847.      * 取得公历节日名称 
  848.      * 
  849.      * @return 公历节日名称,如果不是节日返回空串 
  850.      */  
  851.     public String getSFestivalName() {  
  852.         return this.sFestivalName;  
  853.     }  
  854.   
  855.     /** 
  856.      * 取得农历节日名称 
  857.      * 
  858.      * @return 农历节日名称,如果不是节日返回空串 
  859.      */  
  860.     public String getLFestivalName() {  
  861.         return this.lFestivalName;  
  862.     }  
  863.   
  864.     /** 
  865.      * 是否是农历节日 
  866.      * 
  867.      * @return 是否是农历节日 
  868.      */  
  869.     public boolean isLFestival() {  
  870.         if (!this.isFinded) {  
  871.             this.findFestival();  
  872.         }  
  873.         return this.isLFestival;  
  874.     }  
  875.   
  876.     /** 
  877.      * 是否是公历节日 
  878.      * 
  879.      * @return 是否是公历节日 
  880.      */  
  881.     public boolean isSFestival() {  
  882.         if (!this.isFinded) {  
  883.             this.findFestival();  
  884.         }  
  885.         return this.isSFestival;  
  886.     }  
  887.   
  888.     /** 
  889.      * 是否是节日 
  890.      * 
  891.      * @return 是否是节日 
  892.      */  
  893.     public boolean isFestival() {  
  894.         return this.isSFestival() || this.isLFestival();  
  895.     }  
  896.   
  897.     /** 
  898.      * 是否是放假日 
  899.      * 
  900.      * @return 是否是放假日 
  901.      */  
  902.     public boolean isHoliday() {  
  903.         if (!this.isFinded) {  
  904.             this.findFestival();  
  905.         }  
  906.         return this.isHoliday;  
  907.     }  
  908.   
  909.     /** 
  910.      * 其它日期说明 
  911.      * 
  912.      * @return 日期说明(如:民国2年) 
  913.      */  
  914.     public String getDescription() {  
  915.         if (!this.isFinded) {  
  916.             this.findFestival();  
  917.         }  
  918.         return this.description;  
  919.     }  
  920.   
  921.     /** 
  922.      * 干支字符串 
  923.      * 
  924.      * @param cyclicalNumber 指定干支位置(数字,0为甲子) 
  925.      * @return 干支字符串 
  926.      */  
  927.     private static String getCyclicalString(int cyclicalNumber) {  
  928.         return Lunar.Tianan[Lunar.getTianan(cyclicalNumber)] + Lunar.Deqi[Lunar.getDeqi(cyclicalNumber)];  
  929.     }  
  930.   
  931.     /** 
  932.      * 获得地支 
  933.      * 
  934.      * @param cyclicalNumber 
  935.      * @return 地支 (数字) 
  936.      */  
  937.     private static int getDeqi(int cyclicalNumber) {  
  938.         return cyclicalNumber % 12;  
  939.     }  
  940.   
  941.     /** 
  942.      * 获得天干 
  943.      * 
  944.      * @param cyclicalNumber 
  945.      * @return 天干 (数字) 
  946.      */  
  947.     private static int getTianan(int cyclicalNumber) {  
  948.         return cyclicalNumber % 10;  
  949.     }  
  950.   
  951.     /** 
  952.      * 返回指定数字的农历年份表示字符串 
  953.      * 
  954.      * @param lunarYear 农历年份(数字,0为甲子) 
  955.      * @return 农历年份字符串 
  956.      */  
  957.     private static String getLunarYearString(int lunarYear) {  
  958.         return Lunar.getCyclicalString(lunarYear - 1900 + 36);  
  959.     }  
  960.   
  961.     /** 
  962.      * 返回指定数字的农历月份表示字符串 
  963.      * 
  964.      * @param lunarMonth 农历月份(数字) 
  965.      * @return 农历月份字符串 (例:正) 
  966.      */  
  967.     private static String getLunarMonthString(int lunarMonth) {  
  968.         String lunarMonthString = "";  
  969.         if (lunarMonth == 1) {  
  970.             lunarMonthString = Lunar.lunarString2[4];  
  971.         } else {  
  972.             if (lunarMonth > 9) {  
  973.                 lunarMonthString += Lunar.lunarString2[1];  
  974.             }  
  975.             if (lunarMonth % 10 > 0) {  
  976.                 lunarMonthString += Lunar.lunarString1[lunarMonth % 10];  
  977.             }  
  978.         }  
  979.         return lunarMonthString;  
  980.     }  
  981.   
  982.     /** 
  983.      * 返回指定数字的农历日表示字符串 
  984.      * 
  985.      * @param lunarDay 农历日(数字) 
  986.      * @return 农历日字符串 (例: 廿一) 
  987.      */  
  988.     private static String getLunarDayString(int lunarDay) {  
  989.         if (lunarDay < 1 || lunarDay > 30) {  
  990.             return "";  
  991.         }  
  992.         int i1 = lunarDay / 10;  
  993.         int i2 = lunarDay % 10;  
  994.         String c1 = Lunar.lunarString2[i1];  
  995.         String c2 = Lunar.lunarString1[i2];  
  996.         if (lunarDay < 11) {  
  997.             c1 = Lunar.lunarString2[0];  
  998.         }  
  999.         if (i2 == 0) {  
  1000.             c2 = Lunar.lunarString2[1];  
  1001.         }  
  1002.         return c1 + c2;  
  1003.     }  
  1004. }  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package luozhuangChineseEra;  
  2.   
  3. import java.text.ParseException;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6.   
  7. class MyDate {  
  8.   
  9.     private static int[] dayMonth = {312831303130313130313031};  
  10.     public int day;  
  11.     public int dayCyl;  
  12.     public boolean isLeap;  
  13.     public int monCyl;  
  14.     public int month;  
  15.     public int year;  
  16.     public int yearCyl;  
  17.   
  18.     public MyDate() {  
  19.     }  
  20.   
  21.     public MyDate(int paramInt1, int paramInt2, int paramInt3) {  
  22.         this.year = paramInt1;  
  23.         this.month = paramInt2;  
  24.         this.day = paramInt3;  
  25.     }  
  26.   
  27.     public static int GetNumDayOfMonth(int paramInt1, int paramInt2) {  
  28.         int i = dayMonth[(paramInt2 - 1)];  
  29.         if ((IsBigYear(paramInt1)) && (paramInt2 == 2)) {  
  30.             i++;  
  31.         }  
  32.         return i;  
  33.     }  
  34.   
  35.     public static boolean IsBigYear(int paramInt) {  
  36.         if (paramInt % 400 == 0) {  
  37.             return true;  
  38.         }  
  39.         return (paramInt % 4 == 0) && (paramInt % 100 != 0);  
  40.     }  
  41.   
  42.     public static long DateDays(Date aDate, Date aDate2) {  
  43.         long myTime;  
  44.         long myTime2;  
  45.         long days = 0;  
  46.         myTime = (aDate.getTime() / 1000);  
  47.         // SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd");  
  48.         myTime2 = (aDate2.getTime() / 1000);  
  49.         if (myTime > myTime2) {  
  50.             days = (myTime - myTime2) / (1 * 60 * 60 * 24);  
  51.         } else {  
  52.             days = (myTime2 - myTime) / (1 * 60 * 60 * 24);  
  53.         }  
  54.         return days;  
  55.   
  56.     }  
  57.     // 求2个日期的天数  
  58.   
  59.     public static long DateDays(String date1, String date2) throws ParseException {  
  60.   
  61.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  62.         long myTime;  
  63.         Date aDate2;  
  64.         Date aDate;  
  65.         long myTime2;  
  66.         long days = 0;  
  67.   
  68.         aDate = formatter.parse(date1);// 任意日期,包括当前日期  
  69.         myTime = (aDate.getTime() / 1000);  
  70.   
  71.         // SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd");  
  72.         aDate2 = formatter.parse(date2);// 任意日期,包括当前日期  
  73.         myTime2 = (aDate2.getTime() / 1000);  
  74.   
  75.         if (myTime > myTime2) {  
  76.             days = (myTime - myTime2) / (1 * 60 * 60 * 24);  
  77.         } else {  
  78.             days = (myTime2 - myTime) / (1 * 60 * 60 * 24);  
  79.         }  
  80.   
  81.   
  82.   
  83.         return days;  
  84.   
  85.     }  
  86.   
  87.     // 求2个日期的天数  
  88.     public static long DateDays2(int year1, int month1, int day1, int year2,  
  89.             int month2, int day2) throws ParseException {  
  90.   
  91.         String date1;  
  92.         String date2;  
  93.         date1 = year1 + "-" + month1 + "-" + day1;  
  94.         date2 = year2 + "-" + month2 + "-" + day2;  
  95.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
  96.         long myTime;  
  97.         Date aDate;  
  98.         Date aDate2;  
  99.         long myTime2;  
  100.         long days = 0;  
  101.   
  102.         aDate = formatter.parse(date1);// 任意日期,包括当前日期  
  103.         myTime = (aDate.getTime() / 1000);  
  104.   
  105.         // SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd");  
  106.         aDate2 = formatter.parse(date2);// 任意日期,包括当前日期  
  107.         myTime2 = (aDate2.getTime() / 1000);  
  108.   
  109.         if (myTime > myTime2) {  
  110.             days = (myTime - myTime2) / (1 * 60 * 60 * 24);  
  111.         } else {  
  112.             days = (myTime2 - myTime) / (1 * 60 * 60 * 24);  
  113.         }  
  114.   
  115.   
  116.         return days;  
  117.   
  118.     }  
  119.   
  120.     public int GetNumDayFrom19000101() {  
  121.         int day = 0;  
  122.         try {  
  123.             day = (int) DateDays2(this.year, this.month, this.day, 190011);  
  124.         } catch (ParseException ex) {  
  125.             System.out.println(ex.getMessage());  
  126.         }  
  127.         return day;  
  128.     }  
  129.   
  130.     public long GetSFrom19000101() {  
  131.         return 86400L * (1L + GetNumDayFrom19000101());  
  132.     }  
  133. }  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值