根据年月日(农历)计算四柱(天干地支计年法),java源码
2005-9-22
生肖:鸡
年柱:乙酉
月柱:癸申
日柱:丁未
时柱:庚子
/**
* @Description: 根据年月日(农历)计算四柱(天干地支计年法)
* @return: void
* @Author: estar
* @Date: 2023/7/17 13:50
*/
@Test
public void Test06(){
String ymd="2005-9-22";
System.out.println(ymd);
String[] dateParts = ymd.split("-");
int year = Integer.parseInt(dateParts[0]);
int month = Integer.parseInt(dateParts[1]);
int day = Integer.parseInt(dateParts[2]);
int hour = 0;//时辰(0-23)
String[] heavenlyStems = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
String[] earthlyBranches = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
String[] zodiacs = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
int zodiacIndex = (year - 1900) % 12;
System.out.println("生肖:" + zodiacs[zodiacIndex]);
int heavenlyStemIndex = (year - 4) % 10;
int earthlyBranchIndex = (year - 4) % 12;
System.out.println("年柱:" + heavenlyStems[heavenlyStemIndex] + earthlyBranches[earthlyBranchIndex]);
// 计算月柱
int monthStemIndex = (heavenlyStemIndex * 2 + earthlyBranchIndex + month - 1) % 10;
int monthBranchIndex = (month - 1) % 12;
System.out.println("月柱:" + heavenlyStems[monthStemIndex] + earthlyBranches[monthBranchIndex]);
// 计算日柱
int dayStemIndex = (year * 5 + month * 2 + day + 8) % 10;
int dayBranchIndex = (day + 9) % 12;
System.out.println("日柱:" + heavenlyStems[dayStemIndex] + earthlyBranches[dayBranchIndex]);
// 计算时柱
int hourStemIndex = (dayStemIndex * 2 + hour / 2) % 10;
int hourBranchIndex = hour % 12;
System.out.println("时柱:" + heavenlyStems[hourStemIndex] + earthlyBranches[hourBranchIndex]);
}