根据年月日(农历)计算四柱(天干地支计年法),java源码

根据年月日(农历)计算四柱(天干地支计年法),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]);

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

依星net188.com

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值