1 package com.linying.util;
  2 
  3 import java.text.ParseException;
  4 import java.text.SimpleDateFormat;
  5 import java.util.Calendar;
  6 import java.util.Date;
  7 
  8 public final class LunarUtil {
  9 
 10     private static int year;
 11 
 12     private static int month;
 13 
 14     private static int day;
 15 
 16     private static boolean leap;
 17     
 18     public static String getLunar(Calendar cal){
 19         int yearCyl, monCyl, dayCyl;
 20         int leapMonth = 0;
 21         Date baseDate = null;
 22         try {
 23             baseDate = chineseDateFormat.parse("1900.1.31");
 24         } catch (ParseException e) {
 25             e.printStackTrace(); //To   change   body   of   catch   statement   use   Options   |   File   Templates.   
 26         }
 27 
 28         //求出和1900年1月31日相差的天数   
 29         int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L);
 30         dayCyl = offset + 40;
 31         monCyl = 14;
 32 
 33         //用offset减去每农历年的天数   
 34         //   计算当天是农历第几天   
 35         //i最终结果是农历的年份   
 36         //offset是当年的第几天   
 37         int iYear, daysOfYear = 0;
 38         for (iYear = 1900; iYear < 2050 && offset > 0; iYear++) {
 39             daysOfYear = yearDays(iYear);
 40             offset -= daysOfYear;
 41             monCyl += 12;
 42         }
 43         if (offset < 0) {
 44             offset += daysOfYear;
 45             iYear--;
 46             monCyl -= 12;
 47         }
 48         //农历年份   
 49         year = iYear;
 50 
 51         yearCyl = iYear - 1864;
 52         leapMonth = leapMonth(iYear); //闰哪个月,1-12   
 53         leap = false;
 54 
 55         //用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天   
 56         int iMonth, daysOfMonth = 0;
 57         for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {
 58             //闰月   
 59             if (leapMonth > 0 && iMonth == (leapMonth + 1&& !leap) {
 60                 --iMonth;
 61                 leap = true;
 62                 daysOfMonth = leapDays(year);
 63             } else
 64                 daysOfMonth = monthDays(year, iMonth);
 65 
 66             offset -= daysOfMonth;
 67             //解除闰月   
 68             if (leap && iMonth == (leapMonth + 1))
 69                 leap = false;
 70             if (!leap)
 71                 monCyl++;
 72         }
 73         //offset为0时,并且刚才计算的月份是闰月,要校正   
 74         if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
 75             if (leap) {
 76                 leap = false;
 77             } else {
 78                 leap = true;
 79                 --iMonth;
 80                 --monCyl;
 81             }
 82         }
 83         //offset小于0时,也要校正   
 84         if (offset < 0) {
 85             offset += daysOfMonth;
 86             --iMonth;
 87             --monCyl;
 88         }
 89         month = iMonth;
 90         day = offset + 1;
 91         return chineseNumber[month - 1+ "" + getChinaDayString(day);
 92     }
 93 
 94     public static String getChinaDayString(int day) {
 95         String chineseTen[] = { """""廿""" };
 96         int n = day % 10 == 0 ? 9 : day % 10 - 1;
 97         if (day > 30)
 98             return "";
 99         if (day == 10)
100             return "初十";
101         else
102             return chineseTen[day / 10+ chineseNumber[n];
103     }
104     final static String chineseNumber[] = { """""""""""""",
105             """""""十一""十二" };
106 
107     public static SimpleDateFormat chineseDateFormat = new SimpleDateFormat(
108             "yyyy.MM.dd");
109 
110     final static long[] lunarInfo = new long[] { 0x04bd80x04ae00x0a570,
111             0x054d50x0d2600x0d9500x165540x056a00x09ad00x055d2,
112             0x04ae00x0a5b60x0a4d00x0d2500x1d2550x0b5400x0d6a0,
113             0x0ada20x095b00x149770x049700x0a4b00x0b4b50x06a50,
114             0x06d400x1ab540x02b600x095700x052f20x049700x06566,
115             0x0d4a00x0ea500x06e950x05ad00x02b600x186e30x092e0,
116             0x1c8d70x0c9500x0d4a00x1d8a60x0b5500x056a00x1a5b4,
117             0x025d00x092d00x0d2b20x0a9500x0b5570x06ca00x0b550,
118             0x153550x04da00x0a5d00x145730x052d00x0a9a80x0e950,
119             0x06aa00x0aea60x0ab500x04b600x0aae40x0a5700x05260,
120             0x0f2630x0d9500x05b570x056a00x096d00x04dd50x04ad0,
121             0x0a4d00x0d4d40x0d2500x0d5580x0b5400x0b5a00x195a6,
122             0x095b00x049b00x0a9740x0a4b00x0b27a0x06a500x06d40,
123             0x0af460x0ab600x095700x04af50x049700x064b00x074a3,
124             0x0ea500x06b580x055c00x0ab600x096d50x092e00x0c960,
125             0x0d9540x0d4a00x0da500x075520x056a00x0abb70x025d0,
126             0x092d00x0cab50x0a9500x0b4a00x0baa40x0ad500x055d9,
127             0x04ba00x0a5b00x151760x052b00x0a9300x079540x06aa0,
128             0x0ad500x05b520x04b600x0a6e60x0a4e00x0d2600x0ea65,
129             0x0d5300x05aa00x076a30x096d00x04bd70x04ad00x0a4d0,
130             0x1d0b60x0d2500x0d5200x0dd450x0b5a00x056d00x055b2,
131             0x049b00x0a5770x0a4b00x0aa500x1b2550x06d200x0ada0 };
132 
133     //======   传回农历   y年的总天数   
134     final private static int yearDays(int y) {
135         int i, sum = 348;
136         for (i = 0x8000; i > 0x8; i >>= 1) {
137             if ((lunarInfo[y - 1900& i) != 0)
138                 sum += 1;
139         }
140         return (sum + leapDays(y));
141     }
142 
143     //======   传回农历   y年闰月的天数   
144     final private static int leapDays(int y) {
145         if (leapMonth(y) != 0) {
146             if ((lunarInfo[y - 1900& 0x10000!= 0)
147                 return 30;
148             else
149                 return 29;
150         } else
151             return 0;
152     }
153 
154     //======   传回农历   y年闰哪个月   1-12   ,   没闰传回   0   
155     final private static int leapMonth(int y) {
156         return (int) (lunarInfo[y - 1900& 0xf);
157     }
158 
159     //======   传回农历   y年m月的总天数   
160     final private static int monthDays(int y, int m) {
161         if ((lunarInfo[y - 1900& (0x10000 >> m)) == 0)
162             return 29;
163         else
164             return 30;
165     }
166 
167     //======   传回农历   y年的生肖   
168     final public String animalsYear() {
169         final String[] Animals = new String[] { """""""""""",
170                 """""""""""" };
171         return Animals[(year - 4% 12];
172     }
173 
174     //======   传入   月日的offset   传回干支,   0=甲子   
175     final private static String cyclicalm(int num) {
176         final String[] Gan = new String[] { """""""""""""",
177                 """""" };
178         final String[] Zhi = new String[] { """""""""""""",
179                 """""""""" };
180         return (Gan[num % 10+ Zhi[num % 12]);
181     }
182 
183     //======   传入   offset   传回干支,   0=甲子   
184     final public String cyclical() {
185         int num = year - 1900 + 36;
186         return (cyclicalm(num));
187     }
188     public static void main(String[] args) throws ParseException {
189         Calendar today = Calendar.getInstance();
190         today.setTime(chineseDateFormat.parse("2009.11.15"));
191         //Lunar lunar = new Lunar(today);
192 
193         System.out.println(getLunar(today));
194     }
195     
196 }
197 
posted on 2010-01-24 09:43 Ying-er 阅读(871) 评论(1)  编辑  收藏

评论:
# re: 得到农历日期的工具类 2010-11-25 23:17 | iui
复制的时候,怎么连行号都一起复制了  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: