1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
| package com.wekri.calendar; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;
public class CalendarUtil { private final static int[] LUNAR_INFO = { 0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2, 0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977, 0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970, 0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950, 0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557, 0x06ca0,0x0b550,0x15355,0x04da0,0x0a5d0,0x14573,0x052d0,0x0a9a8,0x0e950,0x06aa0, 0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0, 0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b5a0,0x195a6, 0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570, 0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0, 0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5, 0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930, 0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530, 0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45, 0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0 };
private final static int MIN_YEAR = 1900; private final static int MAX_YEAR = 2049; private static boolean isLeapYear; private final static String START_DATE = "19000130";
private static int getLeapMonth(int year) { return (int) (LUNAR_INFO[year - 1900] & 0xf); }
private static int getLeapMonthDays(int year) { if(getLeapMonth(year)!=0){ if((LUNAR_INFO[year - 1900] & 0xf0000)==0){ return 29; }else { return 30; } }else{ return 0; } }
private static int getMonthDays(int lunarYeay, int month) throws Exception { if ((month > 31) || (month < 0)) { throw(new Exception("月份有错!")); } int bit = 1 << (16-month); if(((LUNAR_INFO[lunarYeay - 1900] & 0x0FFFF)&bit)==0){ return 29; }else { return 30; } }
private static int getYearDays(int year) { int sum = 29*12; for(int i=0x8000;i>=0x8;i>>=1){ if((LUNAR_INFO[year-1900]&0xfff0&i)!=0){ sum++; } } return sum+getLeapMonthDays(year); }
@Deprecated private static int daysBetween2(Date startDate, Date endDate) { long between_days=(endDate.getTime()-startDate.getTime())/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days)); }
private static int daysBetween(Date startDate, Date endDate) { int days = 0; Calendar can1 = Calendar.getInstance(); can1.setTime(startDate); Calendar can2 = Calendar.getInstance(); can2.setTime(endDate); int year1 = can1.get(Calendar.YEAR); int year2 = can2.get(Calendar.YEAR);
Calendar can = null; if(can1.before(can2)){ days -= can1.get(Calendar.DAY_OF_YEAR); days += can2.get(Calendar.DAY_OF_YEAR); can = can1; }else{ days -= can2.get(Calendar.DAY_OF_YEAR); days += can1.get(Calendar.DAY_OF_YEAR); can = can2; } for (int i = 0; i < Math.abs(year2-year1); i++) { days += can.getActualMaximum(Calendar.DAY_OF_YEAR); can.add(Calendar.YEAR, 1); } return days; }
private static void checkLunarDate(int lunarYear, int lunarMonth, int lunarDay, boolean leapMonthFlag) throws Exception { if ((lunarYear < MIN_YEAR) || (lunarYear > MAX_YEAR)) { throw(new Exception("非法农历年份!")); } if ((lunarMonth < 1) || (lunarMonth > 12)) { throw(new Exception("非法农历月份!")); } if ((lunarDay < 1) || (lunarDay > 30)) { throw(new Exception("非法农历天数!")); }
int leap = getLeapMonth(lunarYear); if ((leapMonthFlag == true) && (lunarMonth != leap)) { throw(new Exception("非法闰月!")); } }
public static String lunarToSolar(String lunarDate, boolean leapMonthFlag) throws Exception{ int lunarYear = Integer.parseInt(lunarDate.substring(0, 4)); int lunarMonth = Integer.parseInt(lunarDate.substring(4, 6)); int lunarDay = Integer.parseInt(lunarDate.substring(6, 8));
checkLunarDate(lunarYear, lunarMonth, lunarDay, leapMonthFlag);
int offset = 0;
for (int i = MIN_YEAR; i < lunarYear; i++) { int yearDaysCount = getYearDays(i); offset += yearDaysCount; } int leapMonth = getLeapMonth(lunarYear);
if(leapMonthFlag & leapMonth != lunarMonth){ throw(new Exception("您输入的闰月标志有误!")); }
if(leapMonth==0|| (lunarMonth < leapMonth) || (lunarMonth==leapMonth && !leapMonthFlag)){ for (int i = 1; i < lunarMonth; i++) { int tempMonthDaysCount = getMonthDays(lunarYear, i); offset += tempMonthDaysCount; }
if (lunarDay > getMonthDays(lunarYear, lunarMonth)) { throw(new Exception("不合法的农历日期!")); } offset += lunarDay; }else{ for (int i = 1; i < lunarMonth; i++) { int tempMonthDaysCount = getMonthDays(lunarYear, i); offset += tempMonthDaysCount; } if (lunarMonth>leapMonth) { int temp = getLeapMonthDays(lunarYear); offset += temp;
if (lunarDay > getMonthDays(lunarYear, lunarMonth)) { throw(new Exception("不合法的农历日期!")); } offset += lunarDay; }else { int temp = getMonthDays(lunarYear, lunarMonth); offset += temp;
if (lunarDay > getLeapMonthDays(lunarYear)) { throw(new Exception("不合法的农历日期!")); } offset += lunarDay; } }
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); Date myDate = null; myDate = formatter.parse(START_DATE); Calendar c = Calendar.getInstance(); c.setTime(myDate); c.add(Calendar.DATE, offset); myDate = c.getTime();
return formatter.format(myDate); }
public static String solarToLunar(String solarDate) throws Exception{ int i; int temp = 0; int lunarYear; int lunarMonth; int lunarDay; boolean leapMonthFlag =false;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); Date myDate = null; Date startDate = null; try { myDate = formatter.parse(solarDate); startDate = formatter.parse(START_DATE); } catch (ParseException e) { e.printStackTrace(); }
int offset = daysBetween(startDate,myDate);
for (i = MIN_YEAR; i <= MAX_YEAR; i++){ temp = getYearDays(i); if (offset - temp < 1){ break; }else{ offset -= temp; } } lunarYear = i;
int leapMonth = getLeapMonth(lunarYear); if (leapMonth > 0){ isLeapYear = true; }else{ isLeapYear = false; }
for (i = 1; i<=12; i++) { if(i==leapMonth+1 && isLeapYear){ temp = getLeapMonthDays(lunarYear); isLeapYear = false; leapMonthFlag = true; i--; }else{ temp = getMonthDays(lunarYear, i); } offset -= temp; if(offset<=0){ break; } }
offset += temp; lunarMonth = i; lunarDay = offset;
return "阴历:"+lunarYear+"年"+(leapMonthFlag&(lunarMonth==leapMonth)?"闰":"")+lunarMonth+"月"+lunarDay+"日"; }
}
|