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

摘要

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

工具类介绍

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

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

less 复制代码
System.out.println("今年七夕时间:"  +
new ChineseDate(java.time.Year.now().getValue(), 7, 7)
.getGregorianDate());       

API演示

引入依赖

xml 复制代码
<!-- hutool工具包 -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.29</version>
</dependency>

实现代码

csharp 复制代码
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(), 7, 7)
                        .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源码

java 复制代码
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<>(1, 1), "春节");
    L_FTV.put(new Pair<>(1, 2), "犬日");
    L_FTV.put(new Pair<>(1, 3), "猪日");
    L_FTV.put(new Pair<>(1, 4), "羊日");
    L_FTV.put(new Pair<>(1, 5), "牛日 破五日");
    L_FTV.put(new Pair<>(1, 6), "马日 送穷日");
    L_FTV.put(new Pair<>(1, 7), "人日 人胜节");
    L_FTV.put(new Pair<>(1, 8), "谷日 八仙日");
    L_FTV.put(new Pair<>(1, 9), "天日 九皇会");
    L_FTV.put(new Pair<>(1, 10), "地日 石头生日");
    L_FTV.put(new Pair<>(1, 12), "火日 老鼠娶媳妇日");
    L_FTV.put(new Pair<>(1, 13), "上(试)灯日 关公升天日");
    L_FTV.put(new Pair<>(1, 15), "元宵节 上元节");
    L_FTV.put(new Pair<>(1, 18), "落灯日");

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

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

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

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

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

    // 七月
    L_FTV.put(new Pair<>(7, 7), "七夕");
    L_FTV.put(new Pair<>(7, 14), "鬼节(南方)");
    L_FTV.put(new Pair<>(7, 15), "中元节");
    L_FTV.put(new Pair<>(7, 15), "盂兰盆节 中元节");
    L_FTV.put(new Pair<>(7, 30), "地藏节");

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

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

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

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

    // 腊月
    L_FTV.put(new Pair<>(12, 8), "腊八节");
    L_FTV.put(new Pair<>(12, 16), "尾牙");
    L_FTV.put(new Pair<>(12, 23), "小年");
    L_FTV.put(new Pair<>(12, 30), "除夕");
}

总结

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

关注公众号:咖啡Beans

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

相关推荐
To_OC7 小时前
从一头雾水到跑通全流程:我用一个周末啃透了JWT登录鉴权
前端·后端·http
前端兰博8 小时前
05-Redis
redis·后端
databook9 小时前
从手动检查到自动监控:一个数据质量工作流的实现
后端·python·数据分析
【JAVA】玩家9 小时前
Spring核心原理全解析:从零到生产实战
java·后端·spring
东风破_9 小时前
从 Neo4j 到 GraphRAG:用 Text-to-Cypher 构建图检索 RAG
人工智能·后端
llqbzllll11 小时前
Spring AI 工具调用不是反射一下就结束:用 2.0.1 跑通失败恢复与调用上限
人工智能·后端
狼爷12 小时前
Rust/Go/Java/Python/PHP 大比拼:负载下后端框架到底差多少?
java·后端·编程语言
mantou13212 小时前
我给 AI Agent 做了个「油猴」:让 Claude Code / Codex 直接用你已登录的浏览器
前端·javascript·后端
程序员老赵12 小时前
Docker 部署 DeepSeek Harness:轻松搭建局域网里的 AI Agent 平台
后端·ai编程·deepseek
YIAN13 小时前
实战|用 DeepSeek + SQLite 从零搭建轻量 Text2SQL 查询助手
后端·sqlite·deepseek