From d1f26741bddf6f512d62c0100d42c52be8d37e76 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期六, 06 二月 2021 15:35:40 +0800 Subject: [PATCH] 工具类优化 --- utils/src/main/java/org/yeshi/utils/DateLunarUtil.java | 1076 +++++++++++++++++++++++++++++----------------------------- 1 files changed, 538 insertions(+), 538 deletions(-) diff --git a/utils/src/main/java/org/yeshi/utils/DateLunarUtil.java b/utils/src/main/java/org/yeshi/utils/DateLunarUtil.java index ee94978..d7e3209 100644 --- a/utils/src/main/java/org/yeshi/utils/DateLunarUtil.java +++ b/utils/src/main/java/org/yeshi/utils/DateLunarUtil.java @@ -1,539 +1,539 @@ -package org.yeshi.utils; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; - -import javax.script.Invocable; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; - -import org.yeshi.utils.entity.DateInfo; - -/** - * 銆堝皢鏃ユ湡杞崲涓哄共鏀巻娉曠殑骞存湀鏃ユ椂锛屽苟璁$畻鐢熻倴銆侀槾鍘嗐�佹槦鏈熷嚑銆� - * - */ -public class DateLunarUtil { - - static int[] lunarInfo = new int[] { 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, 0x0a5b0, 0x14573, - 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, - 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 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, 0x14b63 }; - - static String[] Gan = new String[] { "鐢�", "涔�", "涓�", "涓�", "鎴�", "宸�", "搴�", "杈�", "澹�", "鐧�" }; - static String[] Zhi = new String[] { "瀛�", "涓�", "瀵�", "鍗�", "杈�", "宸�", "鍗�", "鏈�", "鐢�", "閰�", "鎴�", "浜�" }; - - static long[] sTermInfo = new long[] { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, - 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, - 504758 }; - - final static String chineseNumber[] = { "姝�", "浜�", "涓�", "鍥�", "浜�", "鍏�", "涓�", "鍏�", "涔�", "鍗�", "鍗佷竴", "鑵�" }; - final static String chineseNumber1[] = { "涓�", "浜�", "涓�", "鍥�", "浜�", "鍏�", "涓�", "鍏�", "涔�", "鍗�", "鍗佷竴", "鍗佷簩" }; - static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy骞碝M鏈坉d鏃�"); - - // 褰撳墠骞� - private static Integer year; - // 褰撳墠鏈� - private static Integer month; - // 褰撳墠鏃� - private static Integer day; - - // 褰撳墠鏄熸湡鍑� - private static String weekDay; - // 鍐滃巻-骞� - private static String lunar_year; - // 鍐滃巻-鏈� - private static String lunar_month; - // 鍐滃巻-鏃� - private static String lunar_day; - - // 澶╁共鍦版敮-骞� - private static String agenary_year; - // 澶╁共鍦版敮-鏈� - private static String agenary_month; - // 澶╁共鍦版敮-鏃� - private static String agenary_day; - // 澶╁共鍦版敮-鏃惰景 - private static String agenary_hour; - - /** - * 鍒嗚褰撳墠鎴� 骞淬�佹湀銆佹棩 - * - * @param date - */ - public static void splitDate(Date date) { - Calendar cal = Calendar.getInstance(); - cal.setTime(date); - year = cal.get(Calendar.YEAR); - month = cal.get(Calendar.MONTH) + 1; - day = cal.get(Calendar.DAY_OF_MONTH); - } - - /** - * 杞崲鎴愰槾鍘� - * - * @param date - * @return - */ - public static String toLunar(Date date) { - Calendar cal = Calendar.getInstance(); - cal.setTime(date); - - int leapMonth = 0; - Date baseDate = null; - try { - baseDate = chineseDateFormat.parse("1900骞�1鏈�31鏃�"); - } catch (ParseException e) { - e.printStackTrace(); // To change body of catch statement use - // Options | File Templates. - } - - // 姹傚嚭鍜�1900骞�1鏈�31鏃ョ浉宸殑澶╂暟 - int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L); - - // 鐢╫ffset鍑忓幓姣忓啘鍘嗗勾鐨勫ぉ鏁� - // 璁$畻褰撳ぉ鏄啘鍘嗙鍑犲ぉ - // i鏈�缁堢粨鏋滄槸鍐滃巻鐨勫勾浠� - // offset鏄綋骞寸殑绗嚑澶� - int iYear, daysOfYear = 0; - for (iYear = 1900; iYear < 2050 && offset > 0; iYear++) { - daysOfYear = lYearDays(iYear); - offset -= daysOfYear; - } - if (offset < 0) { - offset += daysOfYear; - iYear--; - } - - // 鍐滃巻骞翠唤 - int year = iYear; - leapMonth = leapMonth(iYear); // 闂板摢涓湀,1-12 - - boolean leap = false; - // 鐢ㄥ綋骞寸殑澶╂暟offset,閫愪釜鍑忓幓姣忔湀锛堝啘鍘嗭級鐨勫ぉ鏁帮紝姹傚嚭褰撳ぉ鏄湰鏈堢殑绗嚑澶� - int iMonth, daysOfMonth = 0; - for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) { - // 闂版湀 - if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) { - --iMonth; - leap = true; - daysOfMonth = leapDays(year); - } else - daysOfMonth = monthDays(year, iMonth); - - offset -= daysOfMonth; - // 瑙i櫎闂版湀 - if (leap && iMonth == (leapMonth + 1)) - leap = false; - } - // offset涓�0鏃讹紝骞朵笖鍒氭墠璁$畻鐨勬湀浠芥槸闂版湀锛岃鏍℃ - if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) { - if (leap) { - leap = false; - } else { - leap = true; - --iMonth; - } - } - // offset灏忎簬0鏃讹紝涔熻鏍℃ - if (offset < 0) { - offset += daysOfMonth; - --iMonth; - } - - lunar_year = chineseNumberYear(iYear) + "骞�"; - if (leap) { - lunar_month = "闂�" + chineseNumber[iMonth - 1] + "鏈�"; - } else { - lunar_month = chineseNumber[iMonth - 1] + "鏈�"; - } - lunar_day = getChinaDayString(offset + 1); - return lunar_year + lunar_month + lunar_day; - } - - /** - * 灏嗗勾浠借浆鎹负涓枃 - * - * @param year - * @return - */ - public static String chineseNumberYear(int year) { - String year_str = year + ""; - String newYear = ""; - for (int i = 0; i < year_str.length(); i++) { - newYear += formatDigit(year_str.charAt(i)); - } - return newYear; - } - - - public static char formatDigit(char sign) { - if (sign == '0') - sign = '銆�'; - if (sign == '1') - sign = '涓�'; - if (sign == '2') - sign = '浜�'; - if (sign == '3') - sign = '涓�'; - if (sign == '4') - sign = '鍥�'; - if (sign == '5') - sign = '浜�'; - if (sign == '6') - sign = '鍏�'; - if (sign == '7') - sign = '涓�'; - if (sign == '8') - sign = '鍏�'; - if (sign == '9') - sign = '涔�'; - return sign; - } - - public static String getChinaDayString(int day) { - String chineseTen[] = { "鍒�", "鍗�", "寤�", "鍗�" }; - int n = day % 10 == 0 ? 9 : day % 10 - 1; - if (day > 30) - return ""; - - if (day == 10) - return "鍒濆崄"; - - if (day == 20) - return "浜屽崄"; - - if (day == 30) - return "涓夊崄"; - - return chineseTen[day / 10] + chineseNumber1[n]; - } - - - /** - * 璁$畻杈撳嚭鏄熸湡鍑� - * - * @param weekday - * @return - */ - public static String getChinaWeekdayString(String weekday) { - if (weekday.equals("Mon")) - return "涓�"; - if (weekday.equals("Tue")) - return "浜�"; - if (weekday.equals("Wed")) - return "涓�"; - if (weekday.equals("Thu")) - return "鍥�"; - if (weekday.equals("Fri")) - return "浜�"; - if (weekday.equals("Sat")) - return "鍏�"; - if (weekday.equals("Sun")) - return "鏃�"; - else - return ""; - } - - - /** - * 鑾峰彇鏄熸湡鍑犱腑鏂� - * - * @param date - * @return - */ - public static String getWeekd(Date date) { - Calendar calendar = Calendar.getInstance(); - calendar.setTime(date); - weekDay = "鏄熸湡" + getChinaWeekdayString(calendar.getTime().toString().substring(0, 3)); - return weekDay; - } - - - /** - * 杩斿洖鍐滃巻 y骞寸殑鎬诲ぉ鏁� - * - * @param y - */ - public static int lYearDays(int y) { - int i, sum = 348; - for (i = 0x8000; i > 0x8; i >>= 1) { - sum += (lunarInfo[y - 1900] & i) > 0 ? 1 : 0; - } - return (sum + leapDays(y)); - } - - - /** - * 杩斿洖鍐滃巻 y骞撮棸鏈堢殑澶╂暟 - * - * @param y - * @return - */ - public static int leapDays(int y) { - if (leapMonth(y) > 0) { - long day = lunarInfo[y - 1900] & 0x10000; - return day > 0 ? 30 : 29; - } else - return 0; - } - - - /** - * 杩斿洖鍐滃巻 y骞撮棸鍝釜鏈� 1-12 , 娌¢棸杩斿洖 0 - * - * @param y - * @return - */ - public static int leapMonth(int y) { - return (lunarInfo[y - 1900] & 0xf); - } - - - /** - * 杩斿洖鍐滃巻 y骞磎鏈堢殑鎬诲ぉ鏁� - * - * @param y - * @param m - * @return - */ - public static int monthDays(int y, int m) { - return ((lunarInfo[y - 1900] & (0x10000 >> m)) > 0 ? 30 : 29); - } - - final public String animalsYear(int year) { - final String[] Animals = new String[] { "榧�", "鐗�", "铏�", "鍏�", "榫�", "铔�", "椹�", "缇�", "鐚�", "楦�", "鐙�", "鐚�" }; - return Animals[(year - 4) % 12]; - } - - // ===== 鏌愬勾鐨勭n涓妭姘斾负鍑犳棩(浠�0灏忓瘨璧风畻) Date.UTC(1900, 0, 6, 2, 5) - - /** - * 姝g‘鐨勭珛鏄ユ椂闂村簲璇ユ槸浠ュ皬鏃舵潵杩涜璁$畻鐨� - * - * @param y - * @param n - * @return - */ - public static int sTerm(int y, int n) { - long times = 31556925974l * (y - 1900) + sTermInfo[n] * 60000l + (long) 0.7 * (y - 1900); - Date offDate = new Date(times - 2208549300000l); - // 1銆佸彇寰楁湰鍦版椂闂达細 - Calendar cal = Calendar.getInstance(); - cal.setTime(offDate); - // 2銆佸彇寰楁椂闂村亸绉婚噺锛� - int zoneOffset = cal.get(cal.ZONE_OFFSET); - // 3銆佸彇寰楀浠ゆ椂宸細 - int dstOffset = cal.get(cal.DST_OFFSET); - // 4銆佷粠鏈湴鏃堕棿閲屾墸闄よ繖浜涘樊閲忥紝鍗冲彲浠ュ彇寰桿TC鏃堕棿锛� - cal.add(cal.MILLISECOND, -(zoneOffset + dstOffset)); - // 涔嬪悗璋冪敤cal.get(int x)鎴朿al.getTimeInMillis()鏂规硶鎵�鍙栧緱鐨勬椂闂村嵆鏄疷TC鏍囧噯鏃堕棿銆� - return (cal.get(Calendar.DATE)); - } - - /** - * 浼犲叆 offset 杩斿洖骞叉敮, 0=鐢插瓙 - * - * @param num - * @return - */ - public static String cyclical(int num) { - return (Gan[num % 10] + Zhi[num % 12]); - } - - /** - * 璁$畻 骞� 鎵撳嵃 鍏瓧 - * - * @param date - * @throws ParseException - */ - public static String horoscope(Date date) throws ParseException { - Calendar cal = Calendar.getInstance(); - cal.setTime(date); - - int y = cal.get(Calendar.YEAR); - int m = cal.get(Calendar.MONTH); - int i = cal.get(Calendar.DATE) - 1; - - String cY = null; - String cM = null; - String cD = null; - String cH = null; - - // 骞存煴 1900骞寸珛鏄ュ悗涓哄簹瀛愬勾(60杩涘埗36) - if (m < 2) { - cY = cyclical(y - 1900 + 36 - 1); - } else { - cY = cyclical(y - 1900 + 36); - } - int term2 = sTerm(y, 2); // 绔嬫槬鏃ユ湡 - // 鏈堟煴 1900骞�1鏈堝皬瀵掍互鍓嶄负 涓欏瓙鏈�(60杩涘埗12) - int firstNode = sTerm(y, m * 2); // 杩斿洖褰撴湀銆岃妭銆嶄负鍑犳棩寮�濮� - cM = cyclical((y - 1900) * 12 + m + 12); - int dayCyclical = jlday(y, m); - // 渚濊妭姘旇皟鏁翠簩鏈堝垎鐨勫勾鏌�, 浠ョ珛鏄ヤ负鐣� - if (m == 1 && (i + 1) >= term2) - cY = cyclical(y - 1900 + 36); - // 渚濊妭姘旀湀鏌�, 浠ャ�岃妭銆嶄负鐣� - if ((i + 1) >= firstNode) - cM = cyclical((y - 1900) * 12 + m + 13); - // 鏃ユ煴 - cD = cyclical(dayCyclical + i); - - // 鏃舵煴 - int hour = cal.get(Calendar.HOUR_OF_DAY); - cH = Gan[hourG(cD.substring(0, 1), hour)] + Zhi[hourZ(hour)]; - - agenary_year = cY + "骞�"; - agenary_month = cM + "鏈�"; - agenary_day = cD + "鏃�"; - agenary_hour = cH + "鏃�"; - - return agenary_year + agenary_month + agenary_day + agenary_hour; - } - - /** - * 鏍规嵁 鏃ュ共 鎺ㄧ畻 鏃舵煴 鏍规嵁鎻愪緵鐨勬帹绠楀浘鏉ヨ绠� - * - * @param dG - * @param hour - * @return - */ - public static int hourG(String dG, int hour) { - int ind = 1; - for (String s : Gan) { - if (s.equals(dG)) { - break; - } - ind++; - } - ind = ind % 5; // 浜斾釜涓轰竴鍛ㄦ湡 - int hourind = hourZ(hour); - if (hourind > 10) - return hourind - 10 + (ind - 1) * 2; - else { - hourind = hourind + (ind - 1) * 2; - return hourind >= 10 ? hourind - 10 : hourind; - } - } - - /** - * 杩斿洖 灏忔椂瀵瑰簲鐨� 鏀殑绱㈠紩 - * - * @param hour - * @return - */ - public static int hourZ(int hour) { - if (hour >= 23 || hour < 1) - return 0; - else if (hour >= 1 && hour < 3) - return 1; - else if (hour >= 3 && hour < 5) - return 2; - else if (hour >= 5 && hour < 7) - return 3; - else if (hour >= 7 && hour < 9) - return 4; - else if (hour >= 9 && hour < 11) - return 5; - else if (hour >= 11 && hour < 13) - return 6; - else if (hour >= 13 && hour < 15) - return 7; - else if (hour >= 15 && hour < 17) - return 8; - else if (hour >= 17 && hour < 19) - return 9; - else if (hour >= 19 && hour < 21) - return 10; - else if (hour >= 21 && hour < 23) - return 11; - return 0; - } - - /** - * 闂撮殧澶╂暟 - * - * @param y - * @param m - * @return - */ - public static int jlday(int y, int m) { - ScriptEngineManager sem = new ScriptEngineManager(); /* script寮曟搸绠$悊 */ - ScriptEngine se = sem.getEngineByName("javascript"); /* script寮曟搸 */ - try { - se.eval(" var y =" + y + ",m=" + m + " ;"); /* 鎵ц涓�娈祍cript */ - se.eval("function jlday( ) { " + "return Date.UTC(y,m,1,0,0,0,0)/86400000+25567+10;" + "}"); /* 娣诲姞涓�涓柟娉� */ - Invocable invocableEngine = (Invocable) se; - Object callbackvalue = invocableEngine.invokeFunction("jlday"); /* 璋冪敤鏂规硶涓殑鍑芥暟 */ - if (callbackvalue != null) { - double numdb = Double.valueOf(callbackvalue.toString()); - return (int) numdb; - } - } catch (Exception e) { - e.printStackTrace(); - } - return 0; - } - - /** - * 鑾峰彇鏃ユ湡淇℃伅 - * - * @param date - * @return - * @throws ParseException - */ - public static DateInfo getDateInfo(Date date) throws ParseException { - - DateInfo dateInfo = new DateInfo(); - // 鍒嗚鎴愬勾銆佹湀銆佹棩 - splitDate(date); - dateInfo.setDay(day); - dateInfo.setMonth(month); - dateInfo.setYear(year); - - // 鏄熸湡鍑� - getWeekd(date); - dateInfo.setWeekDay(weekDay); - - // 杞崲鎴愬啘鍘嗘棩鏈� - toLunar(date); - dateInfo.setLunar_year(lunar_year); - dateInfo.setLunar_month(lunar_month); - dateInfo.setLunar_day(lunar_day); - - // 澶╁共鍦版敮 - horoscope(date); - dateInfo.setAgenary_year(agenary_year); - dateInfo.setAgenary_month(agenary_month); - dateInfo.setAgenary_day(agenary_day); - dateInfo.setAgenary_hour(agenary_hour); - - return dateInfo; - } - - public static void main(String[] args) throws ParseException { - Date date = new Date(); - - String lunar = DateLunarUtil.toLunar(date); - System.out.println(lunar); - - String horoscope = DateLunarUtil.horoscope(date); - System.out.println(horoscope); - - System.out.println(getWeekd(date)); - - } - +package org.yeshi.utils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; + +import org.yeshi.utils.entity.DateInfo; + +/** + * 銆堝皢鏃ユ湡杞崲涓哄共鏀巻娉曠殑骞存湀鏃ユ椂锛屽苟璁$畻鐢熻倴銆侀槾鍘嗐�佹槦鏈熷嚑銆� + * + */ +public class DateLunarUtil { + + static int[] lunarInfo = new int[] { 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, 0x0a5b0, 0x14573, + 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, + 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 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, 0x14b63 }; + + static String[] Gan = new String[] { "鐢�", "涔�", "涓�", "涓�", "鎴�", "宸�", "搴�", "杈�", "澹�", "鐧�" }; + static String[] Zhi = new String[] { "瀛�", "涓�", "瀵�", "鍗�", "杈�", "宸�", "鍗�", "鏈�", "鐢�", "閰�", "鎴�", "浜�" }; + + static long[] sTermInfo = new long[] { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, + 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, + 504758 }; + + final static String chineseNumber[] = { "姝�", "浜�", "涓�", "鍥�", "浜�", "鍏�", "涓�", "鍏�", "涔�", "鍗�", "鍗佷竴", "鑵�" }; + final static String chineseNumber1[] = { "涓�", "浜�", "涓�", "鍥�", "浜�", "鍏�", "涓�", "鍏�", "涔�", "鍗�", "鍗佷竴", "鍗佷簩" }; + static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy骞碝M鏈坉d鏃�"); + + // 褰撳墠骞� + private static Integer year; + // 褰撳墠鏈� + private static Integer month; + // 褰撳墠鏃� + private static Integer day; + + // 褰撳墠鏄熸湡鍑� + private static String weekDay; + // 鍐滃巻-骞� + private static String lunar_year; + // 鍐滃巻-鏈� + private static String lunar_month; + // 鍐滃巻-鏃� + private static String lunar_day; + + // 澶╁共鍦版敮-骞� + private static String agenary_year; + // 澶╁共鍦版敮-鏈� + private static String agenary_month; + // 澶╁共鍦版敮-鏃� + private static String agenary_day; + // 澶╁共鍦版敮-鏃惰景 + private static String agenary_hour; + + /** + * 鍒嗚褰撳墠鎴� 骞淬�佹湀銆佹棩 + * + * @param date + */ + public static void splitDate(Date date) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + year = cal.get(Calendar.YEAR); + month = cal.get(Calendar.MONTH) + 1; + day = cal.get(Calendar.DAY_OF_MONTH); + } + + /** + * 杞崲鎴愰槾鍘� + * + * @param date + * @return + */ + public static String toLunar(Date date) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + + int leapMonth = 0; + Date baseDate = null; + try { + baseDate = chineseDateFormat.parse("1900骞�1鏈�31鏃�"); + } catch (ParseException e) { + e.printStackTrace(); // To change body of catch statement use + // Options | File Templates. + } + + // 姹傚嚭鍜�1900骞�1鏈�31鏃ョ浉宸殑澶╂暟 + int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L); + + // 鐢╫ffset鍑忓幓姣忓啘鍘嗗勾鐨勫ぉ鏁� + // 璁$畻褰撳ぉ鏄啘鍘嗙鍑犲ぉ + // i鏈�缁堢粨鏋滄槸鍐滃巻鐨勫勾浠� + // offset鏄綋骞寸殑绗嚑澶� + int iYear, daysOfYear = 0; + for (iYear = 1900; iYear < 2050 && offset > 0; iYear++) { + daysOfYear = lYearDays(iYear); + offset -= daysOfYear; + } + if (offset < 0) { + offset += daysOfYear; + iYear--; + } + + // 鍐滃巻骞翠唤 + int year = iYear; + leapMonth = leapMonth(iYear); // 闂板摢涓湀,1-12 + + boolean leap = false; + // 鐢ㄥ綋骞寸殑澶╂暟offset,閫愪釜鍑忓幓姣忔湀锛堝啘鍘嗭級鐨勫ぉ鏁帮紝姹傚嚭褰撳ぉ鏄湰鏈堢殑绗嚑澶� + int iMonth, daysOfMonth = 0; + for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) { + // 闂版湀 + if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) { + --iMonth; + leap = true; + daysOfMonth = leapDays(year); + } else + daysOfMonth = monthDays(year, iMonth); + + offset -= daysOfMonth; + // 瑙i櫎闂版湀 + if (leap && iMonth == (leapMonth + 1)) + leap = false; + } + // offset涓�0鏃讹紝骞朵笖鍒氭墠璁$畻鐨勬湀浠芥槸闂版湀锛岃鏍℃ + if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) { + if (leap) { + leap = false; + } else { + leap = true; + --iMonth; + } + } + // offset灏忎簬0鏃讹紝涔熻鏍℃ + if (offset < 0) { + offset += daysOfMonth; + --iMonth; + } + + lunar_year = chineseNumberYear(iYear) + "骞�"; + if (leap) { + lunar_month = "闂�" + chineseNumber[iMonth - 1] + "鏈�"; + } else { + lunar_month = chineseNumber[iMonth - 1] + "鏈�"; + } + lunar_day = getChinaDayString(offset + 1); + return lunar_year + lunar_month + lunar_day; + } + + /** + * 灏嗗勾浠借浆鎹负涓枃 + * + * @param year + * @return + */ + public static String chineseNumberYear(int year) { + String year_str = year + ""; + String newYear = ""; + for (int i = 0; i < year_str.length(); i++) { + newYear += formatDigit(year_str.charAt(i)); + } + return newYear; + } + + + public static char formatDigit(char sign) { + if (sign == '0') + sign = '銆�'; + if (sign == '1') + sign = '涓�'; + if (sign == '2') + sign = '浜�'; + if (sign == '3') + sign = '涓�'; + if (sign == '4') + sign = '鍥�'; + if (sign == '5') + sign = '浜�'; + if (sign == '6') + sign = '鍏�'; + if (sign == '7') + sign = '涓�'; + if (sign == '8') + sign = '鍏�'; + if (sign == '9') + sign = '涔�'; + return sign; + } + + public static String getChinaDayString(int day) { + String chineseTen[] = { "鍒�", "鍗�", "寤�", "鍗�" }; + int n = day % 10 == 0 ? 9 : day % 10 - 1; + if (day > 30) + return ""; + + if (day == 10) + return "鍒濆崄"; + + if (day == 20) + return "浜屽崄"; + + if (day == 30) + return "涓夊崄"; + + return chineseTen[day / 10] + chineseNumber1[n]; + } + + + /** + * 璁$畻杈撳嚭鏄熸湡鍑� + * + * @param weekday + * @return + */ + public static String getChinaWeekdayString(String weekday) { + if (weekday.equals("Mon")) + return "涓�"; + if (weekday.equals("Tue")) + return "浜�"; + if (weekday.equals("Wed")) + return "涓�"; + if (weekday.equals("Thu")) + return "鍥�"; + if (weekday.equals("Fri")) + return "浜�"; + if (weekday.equals("Sat")) + return "鍏�"; + if (weekday.equals("Sun")) + return "鏃�"; + else + return ""; + } + + + /** + * 鑾峰彇鏄熸湡鍑犱腑鏂� + * + * @param date + * @return + */ + public static String getWeekd(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + weekDay = "鏄熸湡" + getChinaWeekdayString(calendar.getTime().toString().substring(0, 3)); + return weekDay; + } + + + /** + * 杩斿洖鍐滃巻 y骞寸殑鎬诲ぉ鏁� + * + * @param y + */ + public static int lYearDays(int y) { + int i, sum = 348; + for (i = 0x8000; i > 0x8; i >>= 1) { + sum += (lunarInfo[y - 1900] & i) > 0 ? 1 : 0; + } + return (sum + leapDays(y)); + } + + + /** + * 杩斿洖鍐滃巻 y骞撮棸鏈堢殑澶╂暟 + * + * @param y + * @return + */ + public static int leapDays(int y) { + if (leapMonth(y) > 0) { + long day = lunarInfo[y - 1900] & 0x10000; + return day > 0 ? 30 : 29; + } else + return 0; + } + + + /** + * 杩斿洖鍐滃巻 y骞撮棸鍝釜鏈� 1-12 , 娌¢棸杩斿洖 0 + * + * @param y + * @return + */ + public static int leapMonth(int y) { + return (lunarInfo[y - 1900] & 0xf); + } + + + /** + * 杩斿洖鍐滃巻 y骞磎鏈堢殑鎬诲ぉ鏁� + * + * @param y + * @param m + * @return + */ + public static int monthDays(int y, int m) { + return ((lunarInfo[y - 1900] & (0x10000 >> m)) > 0 ? 30 : 29); + } + + final public String animalsYear(int year) { + final String[] Animals = new String[] { "榧�", "鐗�", "铏�", "鍏�", "榫�", "铔�", "椹�", "缇�", "鐚�", "楦�", "鐙�", "鐚�" }; + return Animals[(year - 4) % 12]; + } + + // ===== 鏌愬勾鐨勭n涓妭姘斾负鍑犳棩(浠�0灏忓瘨璧风畻) Date.UTC(1900, 0, 6, 2, 5) + + /** + * 姝g‘鐨勭珛鏄ユ椂闂村簲璇ユ槸浠ュ皬鏃舵潵杩涜璁$畻鐨� + * + * @param y + * @param n + * @return + */ + public static int sTerm(int y, int n) { + long times = 31556925974l * (y - 1900) + sTermInfo[n] * 60000l + (long) 0.7 * (y - 1900); + Date offDate = new Date(times - 2208549300000l); + // 1銆佸彇寰楁湰鍦版椂闂达細 + Calendar cal = Calendar.getInstance(); + cal.setTime(offDate); + // 2銆佸彇寰楁椂闂村亸绉婚噺锛� + int zoneOffset = cal.get(cal.ZONE_OFFSET); + // 3銆佸彇寰楀浠ゆ椂宸細 + int dstOffset = cal.get(cal.DST_OFFSET); + // 4銆佷粠鏈湴鏃堕棿閲屾墸闄よ繖浜涘樊閲忥紝鍗冲彲浠ュ彇寰桿TC鏃堕棿锛� + cal.add(cal.MILLISECOND, -(zoneOffset + dstOffset)); + // 涔嬪悗璋冪敤cal.get(int x)鎴朿al.getTimeInMillis()鏂规硶鎵�鍙栧緱鐨勬椂闂村嵆鏄疷TC鏍囧噯鏃堕棿銆� + return (cal.get(Calendar.DATE)); + } + + /** + * 浼犲叆 offset 杩斿洖骞叉敮, 0=鐢插瓙 + * + * @param num + * @return + */ + public static String cyclical(int num) { + return (Gan[num % 10] + Zhi[num % 12]); + } + + /** + * 璁$畻 骞� 鎵撳嵃 鍏瓧 + * + * @param date + * @throws ParseException + */ + public static String horoscope(Date date) throws ParseException { + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + + int y = cal.get(Calendar.YEAR); + int m = cal.get(Calendar.MONTH); + int i = cal.get(Calendar.DATE) - 1; + + String cY = null; + String cM = null; + String cD = null; + String cH = null; + + // 骞存煴 1900骞寸珛鏄ュ悗涓哄簹瀛愬勾(60杩涘埗36) + if (m < 2) { + cY = cyclical(y - 1900 + 36 - 1); + } else { + cY = cyclical(y - 1900 + 36); + } + int term2 = sTerm(y, 2); // 绔嬫槬鏃ユ湡 + // 鏈堟煴 1900骞�1鏈堝皬瀵掍互鍓嶄负 涓欏瓙鏈�(60杩涘埗12) + int firstNode = sTerm(y, m * 2); // 杩斿洖褰撴湀銆岃妭銆嶄负鍑犳棩寮�濮� + cM = cyclical((y - 1900) * 12 + m + 12); + int dayCyclical = jlday(y, m); + // 渚濊妭姘旇皟鏁翠簩鏈堝垎鐨勫勾鏌�, 浠ョ珛鏄ヤ负鐣� + if (m == 1 && (i + 1) >= term2) + cY = cyclical(y - 1900 + 36); + // 渚濊妭姘旀湀鏌�, 浠ャ�岃妭銆嶄负鐣� + if ((i + 1) >= firstNode) + cM = cyclical((y - 1900) * 12 + m + 13); + // 鏃ユ煴 + cD = cyclical(dayCyclical + i); + + // 鏃舵煴 + int hour = cal.get(Calendar.HOUR_OF_DAY); + cH = Gan[hourG(cD.substring(0, 1), hour)] + Zhi[hourZ(hour)]; + + agenary_year = cY + "骞�"; + agenary_month = cM + "鏈�"; + agenary_day = cD + "鏃�"; + agenary_hour = cH + "鏃�"; + + return agenary_year + agenary_month + agenary_day + agenary_hour; + } + + /** + * 鏍规嵁 鏃ュ共 鎺ㄧ畻 鏃舵煴 鏍规嵁鎻愪緵鐨勬帹绠楀浘鏉ヨ绠� + * + * @param dG + * @param hour + * @return + */ + public static int hourG(String dG, int hour) { + int ind = 1; + for (String s : Gan) { + if (s.equals(dG)) { + break; + } + ind++; + } + ind = ind % 5; // 浜斾釜涓轰竴鍛ㄦ湡 + int hourind = hourZ(hour); + if (hourind > 10) + return hourind - 10 + (ind - 1) * 2; + else { + hourind = hourind + (ind - 1) * 2; + return hourind >= 10 ? hourind - 10 : hourind; + } + } + + /** + * 杩斿洖 灏忔椂瀵瑰簲鐨� 鏀殑绱㈠紩 + * + * @param hour + * @return + */ + public static int hourZ(int hour) { + if (hour >= 23 || hour < 1) + return 0; + else if (hour >= 1 && hour < 3) + return 1; + else if (hour >= 3 && hour < 5) + return 2; + else if (hour >= 5 && hour < 7) + return 3; + else if (hour >= 7 && hour < 9) + return 4; + else if (hour >= 9 && hour < 11) + return 5; + else if (hour >= 11 && hour < 13) + return 6; + else if (hour >= 13 && hour < 15) + return 7; + else if (hour >= 15 && hour < 17) + return 8; + else if (hour >= 17 && hour < 19) + return 9; + else if (hour >= 19 && hour < 21) + return 10; + else if (hour >= 21 && hour < 23) + return 11; + return 0; + } + + /** + * 闂撮殧澶╂暟 + * + * @param y + * @param m + * @return + */ + public static int jlday(int y, int m) { + ScriptEngineManager sem = new ScriptEngineManager(); /* script寮曟搸绠$悊 */ + ScriptEngine se = sem.getEngineByName("javascript"); /* script寮曟搸 */ + try { + se.eval(" var y =" + y + ",m=" + m + " ;"); /* 鎵ц涓�娈祍cript */ + se.eval("function jlday( ) { " + "return Date.UTC(y,m,1,0,0,0,0)/86400000+25567+10;" + "}"); /* 娣诲姞涓�涓柟娉� */ + Invocable invocableEngine = (Invocable) se; + Object callbackvalue = invocableEngine.invokeFunction("jlday"); /* 璋冪敤鏂规硶涓殑鍑芥暟 */ + if (callbackvalue != null) { + double numdb = Double.valueOf(callbackvalue.toString()); + return (int) numdb; + } + } catch (Exception e) { + e.printStackTrace(); + } + return 0; + } + + /** + * 鑾峰彇鏃ユ湡淇℃伅 + * + * @param date + * @return + * @throws ParseException + */ + public static DateInfo getDateInfo(Date date) throws ParseException { + + DateInfo dateInfo = new DateInfo(); + // 鍒嗚鎴愬勾銆佹湀銆佹棩 + splitDate(date); + dateInfo.setDay(day); + dateInfo.setMonth(month); + dateInfo.setYear(year); + + // 鏄熸湡鍑� + getWeekd(date); + dateInfo.setWeekDay(weekDay); + + // 杞崲鎴愬啘鍘嗘棩鏈� + toLunar(date); + dateInfo.setLunar_year(lunar_year); + dateInfo.setLunar_month(lunar_month); + dateInfo.setLunar_day(lunar_day); + + // 澶╁共鍦版敮 + horoscope(date); + dateInfo.setAgenary_year(agenary_year); + dateInfo.setAgenary_month(agenary_month); + dateInfo.setAgenary_day(agenary_day); + dateInfo.setAgenary_hour(agenary_hour); + + return dateInfo; + } + + public static void main(String[] args) throws ParseException { + Date date = new Date(); + + String lunar = DateLunarUtil.toLunar(date); + System.out.println(lunar); + + String horoscope = DateLunarUtil.horoscope(date); + System.out.println(horoscope); + + System.out.println(getWeekd(date)); + + } + } \ No newline at end of file -- Gitblit v1.8.0