当前位置: 首页 > article >正文

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

相关文章:

HarmonyOS APP<<古今职鉴定>>开源教程第20篇:农历日期与节日计算

本篇学习农历算法,实现年俗内容的日期驱动图:农历日期与节日计算 的关键流程与实现要点。 学习目标 完成本篇后,你将能够: ✅ 理解农历算法原理✅ 实现公历转农历✅ 计算传统节日✅ 实现年俗日期匹配 预计学习时间 约 90 分钟…...

5步彻底解决显卡驱动问题:Display Driver Uninstaller完整指南

5步彻底解决显卡驱动问题:Display Driver Uninstaller完整指南 【免费下载链接】display-drivers-uninstaller Display Driver Uninstaller (DDU) a driver removal utility / cleaner utility 项目地址: https://gitcode.com/gh_mirrors/di/display-drivers-unin…...

如何快速掌握LuaJIT字节码还原:面向开发者的完整指南

如何快速掌握LuaJIT字节码还原:面向开发者的完整指南 【免费下载链接】luajit-decompiler https://gitlab.com/znixian/luajit-decompiler 项目地址: https://gitcode.com/gh_mirrors/lu/luajit-decompiler LuaJIT反编译器(LuaJIT Raw-Bytecode D…...

MapTRV2 部署训练与测试(踩坑版本)

1. 背景 目录 1. 背景 1.1 结果 1.1.1 过程截图 存在的坑 安装环境 踩坑记录 过程记录 requirements.txt 设置调试的launch.json数据 合成视频脚本 跑通了MapTRV1 ,想继续跑通MapTRV2,安装运行的时候都存在问题,先从网上找了一些相关的教程,发现教程需要收费,作为白嫖党怎…...

ElevenLabs希腊文语音合成精度提升87%:基于ISO 639-2标准的音素对齐校准全流程详解

更多请点击: https://kaifayun.com 第一章:ElevenLabs希腊文语音合成精度提升87%的工程意义与语言学背景 ElevenLabs在2024年Q2发布的v3.2语音模型中,针对现代希腊语(el-GR)的语音合成MOS(Mean Opinion S…...

【仅剩最后47份】盐印相风格训练数据集泄露报告(含原始Agfa APX 400扫描底片参数+Midjourney反向蒸馏权重)

更多请点击: https://codechina.net 第一章:盐印相风格的视觉基因与数字重生 盐印相(Salted Paper Print)作为19世纪早期摄影术的奠基性工艺,其独特颗粒质感、柔和影调过渡与温润泛黄基底,构成了不可复制的…...

Adobe-GenP 3.0:三步解锁Adobe全家桶的终极破解指南

Adobe-GenP 3.0:三步解锁Adobe全家桶的终极破解指南 【免费下载链接】Adobe-GenP Adobe CC 2019/2020/2021/2022/2023 GenP Universal Patch 3.0 项目地址: https://gitcode.com/gh_mirrors/ad/Adobe-GenP Adobe Creative Cloud的订阅费用让许多设计师望而却…...

5分钟掌握GoReleaser:自动化发布Go项目的终极指南 [特殊字符]

5分钟掌握GoReleaser:自动化发布Go项目的终极指南 🚀 【免费下载链接】goreleaser Release engineering, simplified 项目地址: https://gitcode.com/gh_mirrors/go/goreleaser 还在为每次发布Go项目而烦恼吗?手动构建二进制文件、打包…...

Ladybug天气数据分析工具:建筑环境设计的智能助手

Ladybug天气数据分析工具:建筑环境设计的智能助手 【免费下载链接】ladybug 🐞 Core ladybug library for weather data analysis and visualization 项目地址: https://gitcode.com/gh_mirrors/lad/ladybug Ladybug是一个功能强大的Python天气数…...

vscode使用claude code接入deepseek教程

1 在VSCode拓展商城中搜索Claude Code for VS Code,安装2 快捷键Ctrl“,”,进入设置,选择拓展,选择Claude Code。接着往下拉找到Environment Variables,点击下方的“在settings.json中编辑”,将…...

终极指南:在Debian/Ubuntu系统上快速配置DisplayLink多屏扩展驱动

终极指南:在Debian/Ubuntu系统上快速配置DisplayLink多屏扩展驱动 【免费下载链接】displaylink-debian DisplayLink driver installer for Debian and Ubuntu based Linux distributions. 项目地址: https://gitcode.com/gh_mirrors/di/displaylink-debian …...

Qt 高级开发 009: C++ Lambda 表达式

Qt 高级开发 009: C Lambda 表达式Bilibili 同步视频🔎 一、Lambda 表达式:到底是什么?🧩 二、Lambda 完整结构:六大核心组件1. 捕获列表 [ ] 🎫2. 参数列表 ( ) 📥3. mutable 关键字…...

KMS_VL_ALL_AIO:一键激活Windows与Office的完整解决方案

KMS_VL_ALL_AIO:一键激活Windows与Office的完整解决方案 【免费下载链接】KMS_VL_ALL_AIO Smart Activation Script 项目地址: https://gitcode.com/gh_mirrors/km/KMS_VL_ALL_AIO 你是否曾经为Windows或Office的激活问题而烦恼?每次重装系统后都…...

利用Taotoken CLI工具一键配置团队开发环境中的大模型调用参数

🚀 告别海外账号与网络限制!稳定直连全球优质大模型,限时半价接入中。 👉 点击领取海量免费额度 利用Taotoken CLI工具一键配置团队开发环境中的大模型调用参数 在团队开发环境中,统一管理大模型调用参数是一个常见痛…...

G-ratio Overload

重力加速度比(G-ratio)、过载(Overload)教改最大的特点就是知识与实际相结合,如果在实际生活的体现和应用。 世界一级方程式竞标赛 (F1)...

终极密码学工具箱ToolsFx:30+编码转换与一键解码的完整解决方案

终极密码学工具箱ToolsFx:30编码转换与一键解码的完整解决方案 【免费下载链接】ToolsFx 跨平台密码学工具箱。包含编解码,编码转换,加解密, 哈希,MAC,签名,大数运算,压缩&#xff0…...

Test-Agent:企业级AI测试平台的战略价值与团队转型路径

Test-Agent:企业级AI测试平台的战略价值与团队转型路径 【免费下载链接】Test-Agent Agent that empowers software testing with LLMs; industrial-first in China 项目地址: https://gitcode.com/gh_mirrors/te/Test-Agent 在数字化转型浪潮中,…...

Data Connection (数据连接) 架构设计

description: “移动数据连接 (Data Connection) 与 PDN 会话架构设计,深入剖析 DataNetwork 状态机、数据可用性评估引擎、重试退避算法、以及跨 APN 的并发管理策略。” 当手机完成网络注册(ServiceStateTracker 确定已注册到运营商网络)后,用户最关心的一件事就是:能不…...

本地视频怎么去水印?2026 视频去水印方法与软件推荐指南

概述:为什么要给视频去水印 视频水印是内容平台的标识符,但在某些场景下会影响使用体验——比如下载的视频要用于素材库、制作集锦或进行二次编辑时,水印就成了累赘。本文总结了2026年最实用的本地视频去水印方法,涵盖手机小程序、…...

如何快速自定义游戏光标:提升操作精度的完整指南

如何快速自定义游戏光标:提升操作精度的完整指南 【免费下载链接】YoloMouse Game Cursor Changer 项目地址: https://gitcode.com/gh_mirrors/yo/YoloMouse 在激烈的游戏战斗中,你是否经常因为找不到鼠标光标而错失良机?当屏幕特效绚…...

7.1 DRAM Basics: Internals, Operation

这两段截图是《Memory Systems》一书中关于 DRAM 最基础定义的阐述。我为您提供翻译和深度解读: 1. 中文翻译 图1: 随机存取存储器(RAM)如果每一位使用一个单一的晶体管-电容器对,则被称为动态随机存取存储器(DRAM)。图 7.3 在右下角展示了 DRAM 存储单元的电路。这个电…...

终极指南:如何在Mac上免费快速制作Windows启动盘?

终极指南:如何在Mac上免费快速制作Windows启动盘? 【免费下载链接】windiskwriter 🖥 Windows Bootable USB creator for macOS. 🛠 Patches Windows 11 to bypass TPM and Secure Boot requirements. 👾 UEFI & L…...

Honey Select 2中文汉化补丁终极指南:一键安装完整中文体验

Honey Select 2中文汉化补丁终极指南:一键安装完整中文体验 【免费下载链接】HS2-HF_Patch Automatically translate, uncensor and update HoneySelect2! 项目地址: https://gitcode.com/gh_mirrors/hs/HS2-HF_Patch 还在为Honey Select 2的日语界面而烦恼吗…...

探索AI编程工具的民主化:从技术壁垒到开源共享的技术演进之路

探索AI编程工具的民主化:从技术壁垒到开源共享的技术演进之路 【免费下载链接】cursor-vip cursor IDE enjoy VIP 项目地址: https://gitcode.com/gh_mirrors/cu/cursor-vip "技术不应成为特权,而应是推动文明进步的共同财富。" —— 开…...

PrismLauncher-Cracked:如何通过代码修改实现Minecraft完全离线启动?

PrismLauncher-Cracked:如何通过代码修改实现Minecraft完全离线启动? 【免费下载链接】PrismLauncher-Cracked This project is a Fork of Prism Launcher, which aims to unblock the use of Offline Accounts, disabling the restriction of having a …...

DS4Windows终极指南:如何免费解决手柄漂移并优化游戏操控精度

DS4Windows终极指南:如何免费解决手柄漂移并优化游戏操控精度 【免费下载链接】DS4Windows Like those other ds4tools, but sexier 项目地址: https://gitcode.com/gh_mirrors/ds/DS4Windows 你是否遇到过手柄摇杆自动漂移、瞄准时准星抖动、按键响应延迟等…...

AntiDupl.NET:智能图片去重工具的完整使用指南与实战方案

AntiDupl.NET:智能图片去重工具的完整使用指南与实战方案 【免费下载链接】AntiDupl A program to search similar and defect pictures on the disk 项目地址: https://gitcode.com/gh_mirrors/an/AntiDupl 在数字时代,我们每天都在积累大量的图…...

ElevenLabs蒙古文语音接入全攻略:从API密钥配置到蒙古文音素对齐的7步落地法

更多请点击: https://intelliparadigm.com 第一章:ElevenLabs蒙古文语音接入的背景与技术价值 随着全球多语言AI语音技术加速演进,蒙古语作为联合国教科文组织列为“脆弱型”语言之一,其数字语音合成能力长期受限于高质量语音数据…...

Taotoken API Key管理与访问控制功能在团队大赛中的协作应用

🚀 告别海外账号与网络限制!稳定直连全球优质大模型,限时半价接入中。 👉 点击领取海量免费额度 Taotoken API Key管理与访问控制功能在团队大赛中的协作应用 1. 场景概述:团队协作中的API资源管理需求 当团队共同参…...

【ElevenLabs马来文语音实战指南】:20年AI语音工程师亲授7大避坑要点与本地化发音调优秘技

更多请点击: https://intelliparadigm.com 第一章:ElevenLabs马来文语音技术全景概览 ElevenLabs 作为全球领先的文本转语音(TTS)平台,近年来持续扩展其多语言支持能力,其中马来文(Bahasa Mela…...