자바 양력 전 음력 도구 류

15900 단어 자바 도구
자바 양력 이 음력 으로 바 뀌 는 도구 류 인터넷 에는 많은 것들 이 있 습 니 다. 대체적으로 같은 소스 코드 에서 나 왔 지만 이런 소스 코드 는 프로젝트 요 구 를 만족 시 키 지 못 하고 시간 을 제공 하 는 간지 형식 이 없 기 때문에 직접 프로젝트 수요 에 따라 소스 코드 를 수정 하여 여러분 께 공유 하 였 습 니 다. 문제 가 있 는 부분 은 지적 해 주 십시오.
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * =========================================================
 * 
 *   :dzl
 * 
 *   :1.0
 * 
 *     :2015-7-4   3:24:55
 * 
 *   :       
 * 
 *     :
 * 
 * ==========================================================
 */

public final class Lauar {
	/**
	 *       .year .month .day .isLeap .yearCyl .dayCyl .monCyl
	 */
	private int result[];
	private Calendar calendar;
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy.M.d EEEEE");

	/**
	 * http://blog.csdn.net/onlyonecoder/article/details/8484118
	 */
	private static int[] lunarInfo = { 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 };
	/**
	 *   :http://www.eoeandroid.com/forum.php?mod=viewthread&action=printable&
	 * tid=3075
	 */
	private final 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 };

	private static final String[] SolarTerm = new String[] { "  ", "  ", "  ",
			"  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
			"  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  " };

	/**
	 *   :0-7     ,8-11     ,5-7  12-15   0
	 */
	private final static Map lunarHolidayMap = new HashMap(
			11);
	
	/**
	 *   :0-7     ,8-11     ,5-7  12-15   0
	 */
	private final static Map holidayMap = new HashMap(
			23);

	private static String[] Gan = { " ", " ", " ", " ", " ", " ", " ", " ",
			" ", " " };
	private static String[] Zhi = { " ", " ", " ", " ", " ", " ", " ", " ",
			" ", " ", " ", " " };
	private static String[] Animals = { " ", " ", " ", " ", " ", " ", " ", " ",
			" ", " ", " ", " " };
	private static String[] nStr1 = { " ", " ", " ", " ", " ", " ", " ", " ",
			" ", " ", " " };
	private static String[] nStr2 = { " ", " ", " ", " ", " " };
	private static String[] monthNong = { " ", " ", " ", " ", " ", " ", " ",
			" ", " ", " ", " ", "  ", "  " };
	private static String[] yearName = { " ", " ", " ", " ", " ", " ", " ",
			" ", " ", " " };

	static {
		//     
		lunarHolidayMap.put(0x0101, "  ");
		lunarHolidayMap.put(0x010f, "  ");
		lunarHolidayMap.put(0x0505, "  ");
		lunarHolidayMap.put(0x0707, "  ");
		lunarHolidayMap.put(0x070f, "  ");
		lunarHolidayMap.put(0x080f, "  ");
		lunarHolidayMap.put(0x0909, "  ");
		lunarHolidayMap.put(0x0c08, "  ");
		lunarHolidayMap.put(0x0c18, "  ");
		lunarHolidayMap.put(0x0c1e, "  ");
		
		//    
		holidayMap.put(0x0101, "  ");
		holidayMap.put(0x020e, "   ");
		holidayMap.put(0x0308, "   ");
		holidayMap.put(0x030c, "   ");
		holidayMap.put(0x030f, "      ");
		holidayMap.put(0x0401, "   ");
		holidayMap.put(0x0501, "   ");
		holidayMap.put(0x0504, "   ");
		holidayMap.put(0x050c, "   ");
		holidayMap.put(0x0601, "   ");
		holidayMap.put(0x0701, "   ");
		holidayMap.put(0x0801, "   ");
		holidayMap.put(0x0808, "   ");
		holidayMap.put(0x090a, "   ");
		holidayMap.put(0x091c, "    ");
		holidayMap.put(0x0a01, "   ");
		holidayMap.put(0x0a06, "   ");
		holidayMap.put(0x0a18, "    ");
		holidayMap.put(0x0b0c, "       ");
		holidayMap.put(0x0c14, "      ");
		holidayMap.put(0x0c19, "  ");
	}

	public Lauar() {
		this.calendar = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
		convert();//     
	}

	public Lauar(int year, int month, int day) {
		calendar = Calendar.getInstance();
		calendar.set(year, month - 1, day);
		convert();//     
	}

	/**
	 * @param year
	 * @param month
	 * @param day
	 * @param hourOfDay
	 *            24   (0-23)
	 * @param minute
	 */
	public Lauar(int year, int month, int day, int hourOfDay, int minute) {
		calendar = Calendar.getInstance();
		calendar.set(year, month - 1, day, hourOfDay, minute);
		convert();//     
	}

	public Lauar(Date date) {
		calendar = Calendar.getInstance();
		calendar.setTime(date);
		convert();//     
	}

	public Lauar(Calendar calendar) {
		this.calendar = calendar;
		convert();//     
	}

	/**
	 *         
	 * 
	 * @param year
	 *                
	 * @return
	 */
	private static int totalDaysOfYear(int year) {
		int sum = 348; // 29*12
		for (int i = 0x8000; i > 0x8; i >>= 1) {
			sum += (lunarInfo[year - 1900] & i) == 0 ? 0 : 1; //   +1 
		}
		return (sum + leapDays(year)); // +     
	}

	/**
	 *    year      
	 * 
	 * @param year
	 * @return
	 */
	private static int leapDays(int year) {
		int result = 0;
		if (leapMonth(year) != 0) {
			result = (lunarInfo[year - 1900] & 0x10000) == 0 ? 29 : 30;
		}
		return result;
	}

	/**
	 *     year      1-12 ,      0
	 * 
	 * @param     
	 * @return
	 */
	private static int leapMonth(int year) {
		return (lunarInfo[year - 1900] & 0xf);
	}

	/**
	 *    y m     
	 * 
	 * @param y
	 * @param m
	 * @return
	 */
	private static int monthDays(int y, int m) {
		return ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0 ? 29 : 30);
	}

	/**
	 *            
	 * 
	 * @param date
	 *                
	 */
	private void convert() {
		//      1900-01-31   1900     
		Calendar baseCalendar = Calendar.getInstance();
		baseCalendar.set(1900, 0, 31, 0, 0, 0); // 1900-01-31   1900     
		Date baseDate = baseCalendar.getTime();
		//    ( )
		int offset = (int) ((calendar.getTimeInMillis() - baseDate.getTime()) / 86400000); //   (86400000=24*60*60*1000)
		//                 
		int monCyl = 14; // 1898-10-01      
		int dayCyl = offset + 40; // 1899-12-21   1899      

		//     
		int i;
		int temp = 0;
		for (i = 1900; i < 2050 && offset > 0; i++) {
			temp = totalDaysOfYear(i); //       
			offset -= temp;
			monCyl += 12;
		}
		if (offset < 0) {
			offset += temp;
			i--;
			monCyl -= 12;
		}

		int year = i; //     
		int yearCyl = i - 1864; // 1864     

		int leap = leapMonth(i); //     
		boolean isLeap = false;
		int j;
		for (j = 1; j < 13 && offset > 0; j++) {
			//   
			if (leap > 0 && j == (leap + 1) && isLeap == false) {
				--j;
				isLeap = true;
				temp = leapDays(year);
			} else {
				temp = monthDays(year, j);
			}
			//     
			if (isLeap == true && j == (leap + 1))
				isLeap = false;
			offset -= temp;
			if (isLeap == false)
				monCyl++;
		}
		if (offset == 0 && leap > 0 && j == leap + 1)
			if (isLeap) {
				isLeap = false;
			} else {
				isLeap = true;
				--j;
				--monCyl;
			}
		if (offset < 0) {
			offset += temp;
			--j;
			--monCyl;
		}
		int month = j; //     
		int day = offset + 1; //    
		result = new int[] { year, month, day, isLeap ? 1 : 0, yearCyl, monCyl,
				dayCyl };
	}

	/**
	 *           , 0=  
	 * 
	 * @param num
	 *               ( or or )
	 * @return
	 */
	private static String cyclical(int num) {
		return (Gan[num % 10] + Zhi[num % 12]);
	}

	/**
	 *     
	 * 
	 * @param d
	 * @return
	 */
	private static String chineseDay(int day) {
		String result;
		switch (day) {
			case 10:
				result = "  ";
				break;
			case 20:
				result = "  ";
				break;
			case 30:
				result = "  ";
				break;
			default:
				result = nStr2[(int) (day / 10)];//   
				result += nStr1[day % 10];//   
		}
		return (result);
	}

	/**
	 *     
	 * 
	 * @param y
	 * @return
	 */
	private static String chineseYear(int y) {
		String s = " ";
		int d;
		while (y > 0) {
			d = y % 10;
			y = (y - d) / 10;
			s = yearName[d] + s;
		}
		return (s);
	}

	/**
	 *     :2015.07.04      [ ]         
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public String getLunarDate() {
		String s = sdf.format(calendar.getTime()) + " ";
		s += cyclical(result[4]) + "[" + Animals[(result[0] - 4) % 12] + "]  ";
		s += cyclical(result[5]) + "  ";
		s += cyclical(result[6]) + " ";
		return s;
	}

	/**
	 *     :    
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public String getLunarDay() {
		return (result[3] == 1 ? " " : "") + monthNong[result[1]] + " "
				+ chineseDay(result[2]);
	}
	
	public String getLunarMonth(){
		return (result[3] == 1 ? " " : "") + monthNong[result[1]] + " ";
	}
	
	public String getLunarDayOfMonth(){
		return chineseDay(result[2]);
	}

	/**
	 *     ,    :   
	 * 
	 * @return
	 */
	public String getLunarTime() {
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int timeOffset = (result[6] % 10) * 24 + hour;
		return Gan[((timeOffset + 1) / 2) % 10] + Zhi[((hour + 1) / 2) % 12]
				+ " ";
	}

	/**
	 *          (y m d )    
	 */
	public String getSoralTerm() {
		int y = calendar.get(Calendar.YEAR);
		int m = calendar.get(Calendar.MONTH);
		int d = calendar.get(Calendar.DAY_OF_MONTH);
		String solarTerms;
		if (d == sTerm(y, m * 2))
			solarTerms = SolarTerm[m * 2];
		else if (d == sTerm(y, m * 2 + 1))
			solarTerms = SolarTerm[m * 2 + 1];
		else {
			//           
			solarTerms = null;
		}
		return solarTerms;
	}

	/**
	 * y   n      ( 0    )
	 */
	private int sTerm(int y, int n) {
		Calendar cal = Calendar.getInstance();
		cal.set(1900, 0, 6, 2, 5, 0);
		long temp = cal.getTime().getTime();
		cal.setTimeInMillis((long) ((31556925974.7 * (y - 1900) + STermInfo[n] * 60000L) + temp));
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 *     
	 * 
	 * @return
	 */
	public String getLunarHoliday() {
		int temp;
		if (result[2] == 29 && !isBigMonth(result[0], result[1])) {//     ,29      ,    30  
			temp = (result[1] << 8) + 30;
		}else{
			temp = (result[1] << 8) + result[2];
		}
		return lunarHolidayMap.get(temp);
	}
	
	/**
	 *     
	 * @return
	 */
	public String getHoliday(){
		int m = calendar.get(Calendar.MONTH) + 1;
		int d = calendar.get(Calendar.DAY_OF_MONTH);
		int temp = (m << 8) + d;
		return holidayMap.get(temp);
	}

	/**
	 *   m y        
	 * 
	 * @param y
	 * @param m
	 * @return true:  ,false:  
	 */
	private boolean isBigMonth(int y, int m) {
		return !((lunarInfo[y - 1900] & (0x10000 >> m)) == 0);
	}

	public Calendar getCalendar() {
		return calendar;
	}

	/**
	 *    
	 */
	public void nextDay() {
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		convert();
	}

	/**
	 *    
	 */
	public void preDay() {
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		convert();
	}

	public static void main(String[] args) {
		Lauar lauar = new Lauar(2016, 8, 8);
		System.out.println(lauar.getLunarDate());
		System.out.print(lauar.getLunarDay());
		System.out.print(lauar.getLunarTime());
		System.out.print(lauar.getLunarHoliday());
		System.out.println(lauar.getHoliday());

		lauar.nextDay();
		System.out.println(lauar.getLunarDate());
		System.out.print(lauar.getLunarDay());
		System.out.print(lauar.getLunarTime());
		System.out.print(lauar.getLunarHoliday());
		System.out.println(lauar.getHoliday());

		lauar.preDay();
		System.out.println(lauar.getLunarDate());
		System.out.print(lauar.getLunarDay());
		System.out.print(lauar.getLunarTime());
		System.out.print(lauar.getLunarHoliday());
		System.out.println(lauar.getHoliday());

		lauar = new Lauar(2015, 7, 23);
		System.out.print(lauar.getSoralTerm());
		
	}
}

 실행 결과:
2016.8.8       [ ]         
       null   
2016.8.9       [ ]         
         null
2016.8.8       [ ]         
       null   
  

 
이 도구 클래스 를 사용 하여 간단 한 달력 을 만 듭 니 다:
import java.util.Calendar;

public class GradView {

	private int[] data1;
	private String[] data2;

	public GradView(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, 1);//   1 
		init(calendar);

	}

	private void init(Calendar calendar) {
		int weekOfFirstDay = calendar.get(Calendar.DAY_OF_WEEK);//       

		data1 = new int[42];
		data2 = new String[42];
		Lauar lauar;
		calendar.add(Calendar.DATE, -(weekOfFirstDay - 1));
		for (int i = 0; i < data1.length; i++) {
			data1[i] = calendar.get(Calendar.DAY_OF_MONTH);
			lauar = new Lauar(calendar);

			String data;
			if (null != (data = lauar.getLunarHoliday())) {
				data2[i] = data;
			} else if (null != (data = lauar.getHoliday())) {
				data2[i] = data;
			} else if (null != (data = lauar.getSoralTerm())) {
				data2[i] = data;
			} else {
				data2[i] = lauar.getLunarDayOfMonth();
				if ("  ".equals(data2[i])) {
					data2[i] = lauar.getLunarMonth();
				}
			}

			calendar.add(Calendar.DATE, 1);
		}
	}

	public static void main(String[] args) {
		GradView view = new GradView(2015, 2);
		for (int i = 0; i < view.data1.length; i++) {
			System.out.print(view.data1[i] + ".");
			System.out.print(view.data2[i] + "\t");
			if ((i + 1) % 7 == 0)
				System.out.println();
		}
	}

}

 실행 결과:
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.  	
1.  	2.  	3.  	4.  	5.  	6.  	7.  	
8.   	9.  	10.  	11.  	12.   	13.  	14.  	

 
 
원본 주소:http://blog.csdn.net/wmj2003/article/details/2609370

좋은 웹페이지 즐겨찾기