干货:如何通过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、资讯、面试等多个领域。无论是前沿科技的探索,还是实用技巧的总结,我们都致力于为大家呈现有价值的内容。期待与你共同进步,开启技术之旅。

相关推荐
仙俊红5 分钟前
深入理解 ThreadLocal —— 在 Spring Boot 中的应用与原理
java·spring boot·后端
折七38 分钟前
告别传统开发痛点:AI 驱动的现代化企业级模板 Clhoria
前端·后端·node.js
白衣鸽子42 分钟前
PageHelper:基于拦截器实现的SQL分页查询工具
后端·开源
璨sou43 分钟前
IDE集成开发工具-IDEA
后端
程序员小假44 分钟前
我们来说一说动态代理
java·后端
武子康2 小时前
大数据-108 Flink 流批一体化入门:概念解析与WordCount代码实践 批数据+流数据
大数据·后端·flink
秦禹辰2 小时前
开源多场景问答社区论坛Apache Answer本地部署并发布至公网使用
开发语言·后端·golang
追逐时光者2 小时前
一款开源免费、组件丰富的 WPF UI 控件库,提供了 100 多款常用控件!
后端·.net
小旺不正经2 小时前
数据库表实现账号池管理
数据库·后端·算法
Penge6662 小时前
结构体内存计算:从字段到中文字符深挖
后端