天天看點

java中陰曆和陽曆的轉換

轉載位址:http://blog.csdn.net/gf771115/article/details/6293399?reload

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;


/**
 * 陰陽曆自動轉換工具函數
 * @author admin
 * @date 2013-8-23上午11:12:50
 */
public class Lunar {


private final static int MONTH_DAYS[] = { 29, 30 };
private final static int DAYS_MONTH[][] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
private final static int DATAS[][] = {
{ 23, 3, 2, 17, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0 },
{ 41, 0, 4, 23, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 },
{ 30, 7, 5, 28, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 },
{ 49, 0, 6, 33, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 },
{ 38, 0, 0, 38, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 },
{ 26, 6, 2, 44, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0 },
{ 45, 0, 3, 49, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 35, 0, 4, 54, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 },
{ 24, 4, 5, 59, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1 },
{ 43, 0, 0, 5, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 },
{ 32, 0, 1, 10, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 },
{ 21, 2, 2, 15, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 },
{ 40, 0, 3, 20, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1 },
{ 28, 7, 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 1, 1, 0, 1, 0, 1 },
{ 40, 0, 0, 59, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
{ 28, 6, 2, 5, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0 },
{ 47, 0, 3, 10, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1 },
{ 36, 0, 4, 15, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1 },
{ 25, 5, 5, 20, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0 },
{ 43, 0, 0, 26, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1 },
{ 32, 0, 1, 31, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0 },
{ 22, 3, 2, 36, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0 } };


private static String dateToStr(Date d, int type) {
String s = "";
SimpleDateFormat sdf = null;
switch (type) {
case 1:
sdf = new SimpleDateFormat("yyyy-MM-dd");
case 2:
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
case 3:
sdf = new SimpleDateFormat("yyyyMMddHHmmss");
}
s = sdf.format(d);
return s;
}


/**
* 根據陰曆取得對應的陽曆日期(該日期必須在1936—2028年之間)
* 
* @param date  陰曆日期(字元串)
* @return 傳回對應的陽曆日期
* @throws Exception
*/
public String lunarTosolar(String date) {
Date d = getGregorianCalendar(date);
String s = dateToStr(d, 1);
s = s.substring(0, 4) + "-" + s.substring(4, 6) + "-"
+ s.substring(6, 8);
return s;
}


/**
* 根據陽曆取得對應的陰曆日期(該日期必須在1936—2028年之間)
* 
* @param date  陽曆日期(字元串)
* @return 傳回陰曆日期
* @throws Exception
*/
public String solarTolunar(String date) {
Date d = getLunarCalendar(date);
String s = dateToStr(d, 1);
s = s.substring(0, 4) + "-" + s.substring(4, 6) + "-"
+ s.substring(6, 8);
return s;
}


private static Date getGregorianCalendar(String date) {
Calendar c = getCalendar(date);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int index = year - 1936;
int endIndex = month;


if ((DATAS[index][1] != 0) && (month > DATAS[index][1])) {
endIndex++;
}


int dayNum = 0;


for (int i = 0; i < (endIndex - 1); i++) {
dayNum += MONTH_DAYS[DATAS[index][4 + i]];
}


dayNum += day;
dayNum += DATAS[index][0];


int year_days = 365;


if (isLeapYear(year)) {
year_days = 366;
}


if (dayNum > year_days) {
year++;
dayNum -= year_days;
}


month = 1;


int dayOfMonth[] = DAYS_MONTH[0];


if (isLeapYear(year)) {
dayOfMonth = DAYS_MONTH[1];
}


int i = 0;


for (; i < 12; i++) {
dayNum -= dayOfMonth[i];


if (dayNum <= 0) {
break;
}


month++;
}


day = dayOfMonth[i] + dayNum;


return getDate(year + "-" + month + "-" + day);
}


private static Date getLunarCalendar(String date) {
Calendar calendar = getCalendar(date);
int year = calendar.get(Calendar.YEAR);
int month = 1;
int day;


if ((year < 1936) || (year > 2028)) {
return null;
}


int index = year - 1936;
int l_days = DATAS[index][0];
int day_year = calendar.get(Calendar.DAY_OF_YEAR);
int days;


if (day_year >= l_days) {
days = day_year - l_days;
} else {
index--;
year--;


Calendar c = getCalendar(year + "-12-31");
days = (c.get((Calendar.DAY_OF_YEAR)) + day_year) - DATAS[index][0];
}


int i = 0;
int day_num = 0;


for (; i < 13; i++) {
day_num += MONTH_DAYS[DATAS[index][i + 4]];


if (day_num >= days) {
break;
}


month++;
}


day = MONTH_DAYS[DATAS[index][i + 4]] - (day_num - days);


if ((DATAS[index][1] != 0) && (month > DATAS[index][1])) {
month--;
}


return getDate(year + "-" + month + "-" + day);
}


private static Calendar getCalendar(String date) {
Date dd = getDate(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dd);


return calendar;
}


private static Date getDate(String date) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date dd = null;


try {
dd = format.parse(date);
} catch (Exception e) {
e.printStackTrace();
}


return dd;
}


private static boolean isLeapYear(int year) {
if ((year % 400) == 0) {
return true;
} else if ((year % 100) == 0) {
return false;
} else if ((year % 4) == 0) {
return true;
}


return false;
}

public static void main(String []args){
Lunar lunar = new Lunar();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String nowTime = format.format(date.getTime());
System.out.println(nowTime);
System.out.println(lunar.solarTolunar(nowTime));
System.out.println(lunar.lunarTosolar(nowTime));
}
}