HarmonyOS APP<<古今职鉴定>>开源教程第20篇:农历日期与节日计算
本篇学习农历算法实现年俗内容的日期驱动图农历日期与节日计算 的关键流程与实现要点。学习目标完成本篇后你将能够✅ 理解农历算法原理✅ 实现公历转农历✅ 计算传统节日✅ 实现年俗日期匹配预计学习时间约 90 分钟实战一理解农历算法第一步农历基础知识概念说明农历月大月30天小月29天闰月约每3年一闰闰月跟随前月天干甲乙丙丁戊己庚辛壬癸地支子丑寅卯辰巳午未申酉戌亥第二步农历数据表农历计算需要预置数据表记录每年的月份信息// 农历数据表1900-2100年 // 每个数字的含义 // - 低4位闰月月份0表示无闰月 // - 5-16位每月大小1大0小 // - 17-20位闰月大小 const LUNAR_INFO: number[] [ 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, // ... 更多年份数据 ];第三步农历月份名称const LUNAR_MONTHS: string[] [ 正月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 冬月, 腊月 ]; const LUNAR_DAYS: string[] [ 初一, 初二, 初三, 初四, 初五, 初六, 初七, 初八, 初九, 初十, 十一, 十二, 十三, 十四, 十五, 十六, 十七, 十八, 十九, 二十, 廿一, 廿二, 廿三, 廿四, 廿五, 廿六, 廿七, 廿八, 廿九, 三十 ];实战二实现农历工具类第一步创建 LunarDateUtil// common/LunarDateUtil.ets interface LunarDate { year: number; month: number; day: number; isLeap: boolean; monthName: string; dayName: string; yearName: string; } export class LunarDateUtil { // 农历数据简化版实际需要完整数据 private static LUNAR_INFO: number[] [ 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, // ... 更多数据 ]; private static LUNAR_MONTHS: string[] [ 正月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 冬月, 腊月 ]; private static LUNAR_DAYS: string[] [ 初一, 初二, 初三, 初四, 初五, 初六, 初七, 初八, 初九, 初十, 十一, 十二, 十三, 十四, 十五, 十六, 十七, 十八, 十九, 二十, 廿一, 廿二, 廿三, 廿四, 廿五, 廿六, 廿七, 廿八, 廿九, 三十 ]; private static HEAVENLY_STEMS: string[] [ 甲, 乙, 丙, 丁, 戊, 己, 庚, 辛, 壬, 癸 ]; private static EARTHLY_BRANCHES: string[] [ 子, 丑, 寅, 卯, 辰, 巳, 午, 未, 申, 酉, 戌, 亥 ]; /** * 获取农历年的天干地支 */ static getYearName(year: number): string { const stemIndex (year - 4) % 10; const branchIndex (year - 4) % 12; return this.HEAVENLY_STEMS[stemIndex] this.EARTHLY_BRANCHES[branchIndex] 年; } /** * 获取农历月份名称 */ static getMonthName(month: number, isLeap: boolean): string { return (isLeap ? 闰 : ) this.LUNAR_MONTHS[month - 1]; } /** * 获取农历日期名称 */ static getDayName(day: number): string { return this.LUNAR_DAYS[day - 1]; } /** * 公历转农历简化实现 */ static solarToLunar(year: number, month: number, day: number): LunarDate { // 简化实现基于固定日期计算偏移 // 实际项目中需要完整的农历算法 // 这里使用简化逻辑仅作演示 const baseDate new Date(1900, 0, 31); // 农历1900年正月初一 const targetDate new Date(year, month - 1, day); const offset Math.floor((targetDate.getTime() - baseDate.getTime()) / 86400000); // 简化计算实际需要遍历农历数据表 let lunarYear 1900; let lunarMonth 1; let lunarDay 1; let isLeap false; // 这里省略复杂的计算逻辑 // 实际项目中需要根据 LUNAR_INFO 数据表计算 return { year: lunarYear, month: lunarMonth, day: lunarDay, isLeap: isLeap, monthName: this.getMonthName(lunarMonth, isLeap), dayName: this.getDayName(lunarDay), yearName: this.getYearName(lunarYear) }; } /** * 获取今天的农历日期 */ static getToday(): LunarDate { const now new Date(); return this.solarToLunar(now.getFullYear(), now.getMonth() 1, now.getDate()); } /** * 判断是否是除夕 */ static isNewYearEve(lunar: LunarDate): boolean { // 腊月最后一天 return lunar.month 12 (lunar.day 29 || lunar.day 30); } /** * 判断是否是春节 */ static isSpringFestival(lunar: LunarDate): boolean { return lunar.month 1 lunar.day 1 !lunar.isLeap; } }第二步简化版农历计算由于完整农历算法较复杂这里提供一个简化版本// 简化版基于日期范围判断农历 export class SimpleLunarUtil { /** * 获取当前农历日期描述简化版 * 基于2024年春节日期计算 */ static getCurrentLunarDate(): string { const now new Date(); const year now.getFullYear(); const month now.getMonth() 1; const day now.getDate(); // 2024年春节是2月10日 // 2025年春节是1月29日 // 2026年春节是2月17日 // 简化判断根据公历日期估算农历 if (year 2026) { if (month 2 day 17) { // 春节后 const lunarDay day - 16; return 正月${this.getDayName(lunarDay)}; } else if (month 2 day 17) { // 腊月 const lunarDay day 13; if (lunarDay 30) { return 腊月${this.getDayName(lunarDay)}; } } else if (month 1) { // 腊月 const lunarDay day 13 - 31; if (lunarDay 0 lunarDay 30) { return 腊月${this.getDayName(lunarDay)}; } } } return 农历日期; } private static getDayName(day: number): string { const names [ 初一, 初二, 初三, 初四, 初五, 初六, 初七, 初八, 初九, 初十, 十一, 十二, 十三, 十四, 十五, 十六, 十七, 十八, 十九, 二十, 廿一, 廿二, 廿三, 廿四, 廿五, 廿六, 廿七, 廿八, 廿九, 三十 ]; return names[day - 1] || ; } }实战三年俗日期匹配第一步定义年俗数据interface YearCustom { lunarMonth: number; lunarDay: number; name: string; description: string; } const YEAR_CUSTOMS: YearCustom[] [ { lunarMonth: 12, lunarDay: 23, name: 祭灶, description: 送灶王爷上天 }, { lunarMonth: 12, lunarDay: 24, name: 扫尘, description: 打扫房屋除旧迎新 }, { lunarMonth: 12, lunarDay: 25, name: 磨豆腐, description: 准备年货 }, { lunarMonth: 12, lunarDay: 26, name: 割年肉, description: 准备过年肉食 }, { lunarMonth: 12, lunarDay: 27, name: 宰公鸡, description: 准备年夜饭 }, { lunarMonth: 12, lunarDay: 28, name: 贴花花, description: 贴窗花、年画 }, { lunarMonth: 12, lunarDay: 29, name: 贴春联, description: 贴对联、门神 }, { lunarMonth: 12, lunarDay: 30, name: 守岁, description: 年夜饭、守岁迎新 }, { lunarMonth: 1, lunarDay: 1, name: 拜年, description: 走亲访友拜年 }, { lunarMonth: 1, lunarDay: 2, name: 回娘家, description: 出嫁女儿回娘家 }, { lunarMonth: 1, lunarDay: 3, name: 赤狗日, description: 不宜外出拜年 }, { lunarMonth: 1, lunarDay: 5, name: 破五, description: 迎财神、放鞭炮 }, { lunarMonth: 1, lunarDay: 15, name: 元宵节, description: 赏花灯、吃元宵 } ];第二步匹配今日年俗function getTodayCustom(lunar: LunarDate): YearCustom | null { return YEAR_CUSTOMS.find(custom custom.lunarMonth lunar.month custom.lunarDay lunar.day ) || null; }第三步创建年俗页面interface YearCustom { lunarMonth: number; lunarDay: number; name: string; description: string; } Entry Component struct Lesson20Page { State currentLunarDate: string ; State todayCustom: YearCustom | null null; State allCustoms: YearCustom[] []; private yearCustoms: YearCustom[] [ { lunarMonth: 12, lunarDay: 23, name: 祭灶, description: 送灶王爷上天 }, { lunarMonth: 12, lunarDay: 24, name: 扫尘, description: 打扫房屋除旧迎新 }, { lunarMonth: 12, lunarDay: 25, name: 磨豆腐, description: 准备年货 }, { lunarMonth: 12, lunarDay: 26, name: 割年肉, description: 准备过年肉食 }, { lunarMonth: 12, lunarDay: 27, name: 宰公鸡, description: 准备年夜饭 }, { lunarMonth: 12, lunarDay: 28, name: 贴花花, description: 贴窗花、年画 }, { lunarMonth: 12, lunarDay: 29, name: 贴春联, description: 贴对联、门神 }, { lunarMonth: 12, lunarDay: 30, name: 守岁, description: 年夜饭、守岁迎新 }, { lunarMonth: 1, lunarDay: 1, name: 拜年, description: 走亲访友拜年 }, { lunarMonth: 1, lunarDay: 2, name: 回娘家, description: 出嫁女儿回娘家 }, { lunarMonth: 1, lunarDay: 5, name: 破五, description: 迎财神、放鞭炮 }, { lunarMonth: 1, lunarDay: 15, name: 元宵节, description: 赏花灯、吃元宵 } ]; aboutToAppear() { this.allCustoms this.yearCustoms; // 模拟当前农历日期 this.currentLunarDate 腊月廿九; this.todayCustom this.yearCustoms.find(c c.lunarDay 29 c.lunarMonth 12) || null; } build() { Column() { // 头部 Row() { Text(年俗日历) .fontSize(18) .fontWeight(FontWeight.Bold) .fontColor(#1e293b) } .width(100%) .height(56) .padding({ left: 16, right: 16 }) .backgroundColor(Color.White) Scroll() { Column({ space: 16 }) { // 今日农历 Column({ space: 8 }) { Text(今日农历) .fontSize(14) .fontColor(#64748b) Text(this.currentLunarDate) .fontSize(32) .fontWeight(FontWeight.Bold) .fontColor(#c41e3a) if (this.todayCustom) { Column({ space: 4 }) { Text(this.todayCustom.name) .fontSize(20) .fontWeight(FontWeight.Medium) .fontColor(#1e293b) Text(this.todayCustom.description) .fontSize(14) .fontColor(#64748b) } .margin({ top: 12 }) } } .width(100%) .padding(20) .backgroundColor(Color.White) .borderRadius(12) // 年俗列表 Column({ space: 12 }) { Text(春节年俗) .fontSize(16) .fontWeight(FontWeight.Medium) .fontColor(#1e293b) ForEach(this.allCustoms, (custom: YearCustom) { Row() { Column({ space: 2 }) { Text(custom.lunarMonth 12 ? 腊月${this.getDayName(custom.lunarDay)} : 正月${this.getDayName(custom.lunarDay)}) .fontSize(12) .fontColor(#64748b) Text(custom.name) .fontSize(16) .fontWeight(FontWeight.Medium) .fontColor(#1e293b) } .alignItems(HorizontalAlign.Start) .layoutWeight(1) Text(custom.description) .fontSize(13) .fontColor(#64748b) } .width(100%) .padding(12) .backgroundColor(this.todayCustom?.name custom.name ? #fef2f2 : #f8f8f8) .borderRadius(8) }) } .width(100%) .padding(16) .backgroundColor(Color.White) .borderRadius(12) .alignItems(HorizontalAlign.Start) } .padding(16) } .layoutWeight(1) } .width(100%) .height(100%) .backgroundColor(#f8f6f5) } getDayName(day: number): string { const names [初一, 初二, 初三, 初四, 初五, 初六, 初七, 初八, 初九, 初十, 十一, 十二, 十三, 十四, 十五, 十六, 十七, 十八, 十九, 二十, 廿一, 廿二, 廿三, 廿四, 廿五, 廿六, 廿七, 廿八, 廿九, 三十]; return names[day - 1] || ; } } Builder export function Lesson20PageBuilder() { Lesson20Page() }第四步运行验证hvigorw assembleHap --no-daemon本课小结核心知识点知识点说明农历数据表预置每年月份信息天干地支中国传统纪年法闰月约每3年一闰年俗匹配根据农历日期显示习俗农历计算要点需要预置农历数据表计算公历到农历的偏移处理闰月情况正确显示农历名称课后练习练习1实现完整农历算法使用完整的农历数据表实现准确计算。练习2添加节气计算计算二十四节气日期。下一课预告第21课我们将学习弹窗与对话框设计包括系统弹窗组件自定义弹窗弹窗动画年俗弹窗实现项目开源地址https://gitcode.com/daleishen/gujinzhijian