WindowsPhone 在 根据公历 获取月球日期数据

WindowsPhone 在 它们的定义 类,根据公历 获取月球日期数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ChineseCalendar
{
    /// <summary>
    /// 中国农历
    /// </summary>
    public static class ChineseDate
    {
        #region 日历数据
        /// <summary>
        /// int[] CalendarData
        /// </summary>
        static int[] CalendarData = new int[] {
                    0x41A95, 0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B,
                    0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5,
                    0x90AB5, 0x4BA, 0xA5B, 0x60A57, 0x52B, 0xA93, 0x40E95 };         /// <summary>
        /// int[] madd
        /// </summary>
        static int[] madd = new int[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };         static String numString = "一二三四五六七八九十";
        static String monString = "正二三四五六七八九十冬腊";
        static String[] run = new String[] { "闰正", "闰二", "闰三", "闰四", "闰五", "闰六", "闰七", "闰八", "闰九", "闰十", "闰冬", "闰腊" };
        static String cDateString = String.Empty;
        static int cYear, cMonth, cDay, cHour;
        static String holiday = String.Empty;
        #endregion         #region 节日数据
        /// <summary>
        /// 农历节日数据
        /// </summary>
        static String[] NLHoliday = new String[] { 
                "正月初一 春节",
                "正月十五 元宵节",
                "二月初二 龙抬头",
                "五月初五 端午节",
                "七月初七 七夕",
                "七月十五 中元节",
                "八月十五 中秋节",
                "九月初九 重阳节",
                "腊月初八 腊八节",
                "腊月廿三 小年",
                "腊月三十 除夕"
        };         //公历节日 表示放假日
        static String[] GLHoliday = new String[] {
                "0101 元旦",
                "0214 情人节",
                "0308 妇女节",
                "0312 植树节",
                "0401 愚人节",
                "0422 地球日",
                "0501 劳动节",
                "0504 青年节",
                "0531 无烟日",
                "0601 儿童节",
                "0606 爱眼日",
                "0701 建党日",
                "0707 抗战纪念日",
                "0801 建军节",
                "0910 教师节",
                "0918 九一八事变",
                "1001 国庆节",
                "1031 万圣节",
                "1111 光棍节",
                "1201 艾滋病日",
                "1213 南京大屠杀纪念日",
                "1224 平安夜",
                "1225 圣诞节"
        };
        #endregion         #region 计算方法
        /// <summary>
        /// int GetBit
        /// </summary>
        /// <param name="m"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        private static int GetBit(int m, int n)
        {
            return (m >> n) & 1;
        }         /// <summary>
        /// void e2c
        /// </summary>
        /// <param name="dt">公历时间</param>
        private static void e2c(DateTime dt)
        {
            bool isEnd = false;
            int total, m, n, k, tmp = dt.Year;             if (tmp < 1900) tmp += 1900;             total = (tmp - 2001) * 365
                  + (int)Math.Floor((Double)(tmp - 2001) / 4)
                  + madd[dt.Month - 1]
                  + dt.Day
                  - 23;             if (dt.Year % 4 == 0 && dt.Month > 2)
                total++;             for (m = 0; ; m++)
            {
                k = (CalendarData[m] < 0xfff) ? 11 : 12;
                for (n = k; n >= 0; n--)
                {
                    if (total <= 29 + GetBit(CalendarData[m], n))
                    {
                        isEnd = true;
                        break;
                    }
                    total = total - 29 - GetBit(CalendarData[m], n);
                }
                if (isEnd) break;
            }             cYear = 2001 + m;
            cMonth = k - n + 1;
            cDay = total;             if (k == 12)
            {
                if (cMonth == (int)Math.Floor((double)(CalendarData[m] / 0x10000) + 1))
                    cMonth = 1 - cMonth;                 if (cMonth > (int)Math.Floor((double)(CalendarData[m] / 0x10000) + 1))
                    cMonth--;
            }
            cHour = (int)Math.Floor((double)((dt.Hour + 3) / 2));
        }         /// <summary>
        /// void GetcDateString()
        /// </summary>
        private static void GetcDateString()
        {
            String tmp = String.Empty;             if (cMonth < 1)
            {
                tmp += "闰";
                tmp += monString[-cMonth - 1];
            }
            else tmp += monString[cMonth - 1] + "月";             if (cDay <= 10) tmp += "初";
            else if (cDay < 20) tmp += "十";
            else if (cDay == 20) tmp += "二十";
            else if (cDay < 30) tmp += "廿";
            else tmp += "三十";             if (cDay % 10 != 0 || cDay == 10)
                tmp += numString[Math.Abs(cDay - 1) % 10];             cDateString = tmp;
        }         /// <summary>
        /// 获取相应日期的节日数据
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="strDate"></param>
        private static void getHoliday(DateTime dt, String strDate)
        {
            //获取农历节日
            String strNLHoliday = String.Empty;
            if (strDate == "正月")
                strNLHoliday = "春节";
            else
            {
                for (int i = 0, len = NLHoliday.Length; i < len; i++)
                {
                    if (NLHoliday[i].ToString().Contains(strDate))
                    {
                        strNLHoliday = NLHoliday[i].ToString().Split(' ')[1];
                        break;
                    }
                }
            }             //获取公历节日
            String strGLHoliday = String.Empty;
            for (int i = 0, len = GLHoliday.Length; i < len; i++)
            {
                if (GLHoliday[i].ToString().Contains(dt.ToString("MMdd")))
                {
                    strGLHoliday = GLHoliday[i].ToString().Split(' ')[1];
                    break;
                }
            }             //返回结果
            if (!String.IsNullOrEmpty(strNLHoliday) && !String.IsNullOrEmpty(strGLHoliday))
                holiday = (strNLHoliday + strGLHoliday).Replace("节", "");
            else if (!String.IsNullOrEmpty(strNLHoliday))
                holiday = strNLHoliday;
            else if (!String.IsNullOrEmpty(strGLHoliday))
                holiday = strGLHoliday;
            else holiday = String.Empty;
        }         #region 农历年
        /// <summary>
        /// 十天干
        /// </summary>
        private static string[] tiangan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };         /// <summary>
        /// 十二地支
        /// </summary>
        private static string[] dizhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };         /// <summary>
        /// 十二生肖
        /// </summary>
        private static string[] shengxiao = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };         /// <summary>
        /// 返回农历天干地支年 
        /// </summary>
        /// <param name="year">农历年</param>
        /// <returns></returns>
        private static string GetLunisolarYear(int year)
        {
            int tgIndex = (year - 1900 + 36) % 10;
            int dzIndex = (year - 1900 + 36) % 12;             return string.Concat(tiangan[tgIndex], dizhi[dzIndex], "【", shengxiao[dzIndex], "】");
        }
        #endregion
        #endregion         #region 调用方法
        /// <summary>
        /// 获取农历日期(格式 dd)
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static String getNLDate(DateTime dt)
        {
            e2c(dt);
            GetcDateString();             //获取节日
            getHoliday(dt, cDateString);             if (cDateString.Contains("初一"))
                cDateString = cDateString.Replace("初一", "");
            else
                cDateString = cDateString.Substring(cDateString.IndexOf("月") + 1);             for (int i = 0, len = run.Length; i < len; i++)
            {
                if (cDateString.Contains(run[i]))
                {
                    cDateString = cDateString.Replace(run[i], "");
                    if (String.IsNullOrEmpty(cDateString))
                        cDateString = run[i];                     break;
                }
            }             return cDateString;
        }         /// <summary>
        /// 获取农历日期(格式 年月日完整日期)
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static String getNLDateTime(DateTime dt)
        {
            //农历月
            getNLDate(dt);             return GetLunisolarYear(dt.Year) + "年" + cDateString;
        }         /// <summary>
        /// 获取节日数据
        /// </summary>
        /// <returns></returns>
        public static String getHoliday()
        {
            return holiday;
        }
        #endregion
    }
}

备注:此类 是更依据网上的 javascript 改编而来。

经过測试,当输入的公历 年 小于 2001 年 获取的农历日期数据就会有误。

而且 当 输入的年 大于 2040 年后 获取的农历数据也会有误!

希望有人可以完好。谢谢。

效果图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWltaXl1YW5nZ3Vhbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

WindowsPhone 在 根据公历 获取月球日期数据的更多相关文章

  1. SQL Server使用convert对datetime日期数据进行获取

    来源:http://database.51cto.com/art/201007/211883.htm 备注:本文的语法讲解确实是比较乱,似乎格式不太严谨.参考时还是以实例验证为准比较好 以下的文章主要 ...

  2. C# 获取农历日期

    //C# 获取农历日期 ///<summary> /// 实例化一个 ChineseLunisolarCalendar ///</summary> private static ...

  3. 通过DialogFragment从DatePicker或TimePicker中获取日期数据

    通过DialogFragment从DatePicker或TimePicker中获取日期数据 一个activity类,里面存有date和time的变量,想通过dialogfragment的方式获取用户输 ...

  4. iOS 获取公历、农历日期的年月日

    iOS 获取公历.农历日期的年月日 介绍三种方法获取 Date (NSDate) 的年月日. 用 date 表示当前日期.测试日期为公历 2017 年 2 月 5 日,农历丁酉年,鸡年,正月初九. l ...

  5. 获取指定日期相关DATENAME和DATEPART数据

    DATENAME和DATEPART有何区别,Insus.NET写成一个函数,可以方便查询与对比: 一个是返回一个字符串,另一个是返回一个整数. SET ANSI_NULLS ON GO SET QUO ...

  6. 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期

    腾讯云图片鉴黄集成到C#   官方文档:https://cloud.tencent.com/document/product/641/12422 请求官方API及签名的生成代码如下: public c ...

  7. C标准函数库中获取时间与日期、对时间与日期数据操作及格式化

    表示时间的三种数据类型[编辑] 日历时间(calendar time),是从一个标准时间点(epoch)到现在的时间经过的秒数,不包括插入闰秒对时间的调整.开始计时的标准时间点,各种编译器一般使用19 ...

  8. HR开发 获取信息类型数据

    1.PNP逻辑数据库. LOOP获取信息类型数据. TABLES: PERNR . , . START-OF-SELECTION. GET PERNR . LOOP AT P0000 WHERE .. ...

  9. poi-处理excel的单元格日期数据

    poi处理excel时,当excel没有明确指明是哪个类型的数据时,poi很可能处理单元格的日期数据时就有可能是一串数字.而使用java程序基本无法转换 以下为对poi处理日期情况一些方面的处理(不是 ...

随机推荐

  1. cronjob不跑得原因

    能是环境的不同,能够在cronjob中加个env > /tmp/env.output查看 应用要同一时候输出标准错误合标准输出到一个文件能够&> /tmp/t

  2. Selenium来抓取动态加载的页面

    一般的爬虫都是直接使用http协议,下载指定url的html内容,并对内容进行分析和抽取.在我写的爬虫框架webmagic里也使用了HttpClient来完成这样的任务. 但是有些页面是通过js以及a ...

  3. cocos2dx-3.0(1)------win7 32位android环境搭建

    參照链接http://blog.csdn.net/wonengxing/article/details/23601359 ----我的生活,我的点点滴滴!! 一. Android工具安装 1. 安装J ...

  4. mysql基础:登录退出,修改用户密码,添加删除用户

    今天刚开始学习mysql,最先接触用户管理,给大家分享下 注:mysql中命令的大小写都可以的 ==========登录退出相关=================== root@jack-deskto ...

  5. Windows下visual studio code搭建golang开发环境

    Windows下visual studio code搭建golang开发环境 序幕 其实环境搭建没什么难的,但是遇到一些问题,主要是有些网站资源访问不了(如:golang.org),导致一些包无法安装 ...

  6. 基于WEB 的认证防火墙的设计

    项目要求: 1.  采用Linux iptbles作为接入防火墙,默认放行所以访问入口的80端口 2.  访问者通过http://x.x.x.x 访问防火墙的认证系统,进行账号的登陆操作,同时系统对用 ...

  7. Python使用时间戳

    1.将字符串的时间转换为时间戳 方法: a = "2013-10-10 23:40:00" 将其转换为时间数组 importtime timeArray = time.strpti ...

  8. cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第二步---编辑器(1)--触摸加入点

    /* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦,他说:随便写,第一别全然照搬代码:第二能够说 ...

  9. [C++/CLI编程宝典][2]什么是C++/CLI语言

    对于什么是C++/CLI,我们首先能够简单的将其名字划分为两部分来理解,第一,C++,我们熟悉的眼下被广泛使用的面向对象的ISO国际标准的高级语言,也称为ISOC++,我们这里以后均称其为ISOC++ ...

  10. JAVA 根据经纬度算出附近的正方形的四个角的经纬度

    /** * * @param longitude 经度 * @param latitude 纬度 * @param distance 范围(米) * @return */ public static ...