干货:如何通过Hutool计算出2025年七夕节的时间?

175 阅读3分钟

摘要

本文介绍Hutool里的农历日期工具类ChineseDate的用法,包含生肖、天干地支、传统节日等方法。

工具类介绍

农历日期-ChineseDate农历日期,提供了生肖、天干地支、传统节日等方法,最大支持到2099年。

一行代码查今年七夕时间:2025-08-29 00:00:00

System.out.println("今年七夕时间:"  +
new ChineseDate(java.time.Year.now().getValue(), 77)
.getGregorianDate());       

API演示

引入依赖

<!-- hutool工具包 -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.29</version>
</dependency>

实现代码

package org.coffeebeans.service;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateUtil;

/**
 * <li>ClassName: ChineseDateUse </li>
 * <li>Author: OakWang </li>
 */
public class ChineseDateUse {
    public static void main(String[] args) {
        // 今年七夕时间:2025-08-29 00:00:00
        System.out.println("今年七夕时间:"  +
                new ChineseDate(java.time.Year.now().getValue(), 77)
                        .getGregorianDate());

        //通过农历构建
        ChineseDate chineseDate = new ChineseDate(2025,8,29);
        // 干支纪年信息:乙巳年丙戌月壬戌日
        System.out.println("干支纪年信息:" + chineseDate.getCyclicalYMD());

        //通过公历构建
        ChineseDate date = new ChineseDate(DateUtil.parseDate("2025-08-29"));
        // 农历月份:七月
        System.out.println("农历月份:" + date.getChineseMonth());
        // 农历月称呼:七月
        System.out.println("农历月称呼:" + date.getChineseMonthName());
        // 是否为闰月:false
        System.out.println("是否为闰月:" + date.isLeapMonth());
        // 农历日:初七
        System.out.println("农历日:" + date.getChineseDay());
        // 节气:只会在当天是二十四节气之一时才返回名称,其余日期返回 null
        System.out.println("节气:" + chineseDate.getTerm());
        // 天干地支:乙巳
        System.out.println("天干地支:" + date.getCyclical());
        // 生肖:蛇
        System.out.println("生肖:" + date.getChineseZodiac());
        // 传统节日:七夕
        System.out.println("传统节日:" + date.getFestivals());
        // 公历的日期:2025-08-29 00:00:00
        System.out.println("公历的日期:" + date.getGregorianDate());
        // 农历字符串:乙巳蛇年 七月初七
        System.out.println("农历字符串:" + date);
    }
}

Hutool源码

package cn.hutool.core.date.chinese;
package cn.hutool.core.date.ChineseDate;

private static final String[] GAN = new String[]{"甲""乙""丙""丁""戊""己""庚""辛""壬""癸"};
private static final String[] ZHI = new String[]{"子""丑""寅""卯""辰""巳""午""未""申""酉""戌""亥"};

private static final String[] MONTH_NAME = {"一""二""三""四""五""六""七""八""九""十""十一""十二"};
private static final String[] MONTH_NAME_TRADITIONAL = {"正""二""三""四""五""六""七""八""九""寒""冬""腊"};

private static final String[] TERMS = {
       "小寒""大寒""立春""雨水""惊蛰""春分",
       "清明""谷雨""立夏""小满""芒种""夏至",
       "小暑""大暑""立秋""处暑""白露""秋分",
       "寒露""霜降""立冬""小雪""大雪""冬至"
};

static {
    // 节日
    L_FTV.put(new Pair<>(11), "春节");
    L_FTV.put(new Pair<>(12), "犬日");
    L_FTV.put(new Pair<>(13), "猪日");
    L_FTV.put(new Pair<>(14), "羊日");
    L_FTV.put(new Pair<>(15), "牛日 破五日");
    L_FTV.put(new Pair<>(16), "马日 送穷日");
    L_FTV.put(new Pair<>(17), "人日 人胜节");
    L_FTV.put(new Pair<>(18), "谷日 八仙日");
    L_FTV.put(new Pair<>(19), "天日 九皇会");
    L_FTV.put(new Pair<>(110), "地日 石头生日");
    L_FTV.put(new Pair<>(112), "火日 老鼠娶媳妇日");
    L_FTV.put(new Pair<>(113), "上(试)灯日 关公升天日");
    L_FTV.put(new Pair<>(115), "元宵节 上元节");
    L_FTV.put(new Pair<>(118), "落灯日");

    // 二月
    L_FTV.put(new Pair<>(21), "中和节 太阳生日");
    L_FTV.put(new Pair<>(22), "龙抬头");
    L_FTV.put(new Pair<>(212), "花朝节");
    L_FTV.put(new Pair<>(219), "观世音圣诞");

    // 三月
    L_FTV.put(new Pair<>(33), "上巳节");

    // 四月
    L_FTV.put(new Pair<>(41), "祭雹神");
    L_FTV.put(new Pair<>(44), "文殊菩萨诞辰");
    L_FTV.put(new Pair<>(48), "佛诞节");

    // 五月
    L_FTV.put(new Pair<>(55), "端午节 端阳节");

    // 六月
    L_FTV.put(new Pair<>(66), "晒衣节 姑姑节");
    L_FTV.put(new Pair<>(66), "天贶节");
    L_FTV.put(new Pair<>(624), "彝族火把节");

    // 七月
    L_FTV.put(new Pair<>(77), "七夕");
    L_FTV.put(new Pair<>(714), "鬼节(南方)");
    L_FTV.put(new Pair<>(715), "中元节");
    L_FTV.put(new Pair<>(715), "盂兰盆节 中元节");
    L_FTV.put(new Pair<>(730), "地藏节");

    // 八月
    L_FTV.put(new Pair<>(815), "中秋节");

    // 九月
    L_FTV.put(new Pair<>(99), "重阳节");

    // 十月
    L_FTV.put(new Pair<>(101), "祭祖节");
    L_FTV.put(new Pair<>(1015), "下元节");

    // 十一月
    L_FTV.put(new Pair<>(1117), "阿弥陀佛圣诞");

    // 腊月
    L_FTV.put(new Pair<>(128), "腊八节");
    L_FTV.put(new Pair<>(1216), "尾牙");
    L_FTV.put(new Pair<>(1223), "小年");
    L_FTV.put(new Pair<>(1230), "除夕");
}

总结

以上我们了解了Hutool里的农历日期工具类ChineseDate的用法,包含生肖、天干地支、传统节日等方法,用起来很有意思。

关注公众号:咖啡Beans

在这里,我们专注于软件技术的交流与成长,分享开发心得与笔记,涵盖编程、AI、资讯、面试等多个领域。无论是前沿科技的探索,还是实用技巧的总结,我们都致力于为大家呈现有价值的内容。期待与你共同进步,开启技术之旅。