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