微信平台开发——日历服务

微信平台开发——日历服务

             

                   很多人可能用过如下的功能:

                         微信平台开发——日历服务

                我向微信号发个字符,然后后台去解析字符,拆出其中的关键字,然后去数据库查询是否开启此项服务,如果服务开启,则返回给用户调用此服务的结果。

            最近两天开始做的服务有,天气查询,日历,快递,火车,黄金。。。等六个服务做成接口,今天要分析的是这里面唯一没有调用外部API接口的服务。

                 首先,我们要写好一个计算农历的方法,:

               

#region 获取农历方法

        

        ///<summary>
        /// 实例化一个 ChineseLunisolarCalendar
        ///</summary>
        private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();

        ///<summary>
        /// 十天干
        ///</summary>
        private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };

        ///<summary>
        /// 十二地支
        ///</summary>
        private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };

        ///<summary>
        /// 十二生肖
        ///</summary>
        private static string[] sx = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

        ///<summary>
        /// 返回农历天干地支年
        ///</summary>
        ///<param name="year">农历年</param>
        ///<return s></return s>
        public static string GetLunisolarYear(int year)
        {
            if (year > 3)
            {
                int tgIndex = (year - 4) % 10;
                int dzIndex = (year - 4) % 12;

                return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
            }

            throw new ArgumentOutOfRangeException("无效的年份!");
        }

        ///<summary>
        /// 农历月
        ///</summary>

        ///<return s></return s>
        private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };

        ///<summary>
        /// 农历日
        ///</summary>
        private static string[] days1 = { "初", "十", "廿", "三" };
        ///<summary>
        /// 农历日
        ///</summary>
        private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };


        ///<summary>
        /// 返回农历月
        ///</summary>
        ///<param name="month">月份</param>
        ///<return s></return s>
        public static string GetLunisolarMonth(int month)
        {
            if (month < 13 && month > 0)
            {
                return months[month - 1];
            }

            throw new ArgumentOutOfRangeException("无效的月份!");
        }

        ///<summary>
        /// 返回农历日
        ///</summary>
        ///<param name="day">天</param>
        ///<return s></return s>
        public static string GetLunisolarDay(int day)
        {
            if (day > 0 && day < 32)
            {
                if (day != 20 && day != 30)
                {
                    return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
                }
                else
                {
                    return string.Concat(days[(day - 1) / 10], days1[1]);
                }
            }

            throw new ArgumentOutOfRangeException("无效的日!");
        }



        ///<summary>
        /// 根据公历获取农历日期
        ///</summary>
        ///<param name="datetime">公历日期</param>
        ///<return s></return s>
        public static string GetChineseDateTime(DateTime datetime)
        {
            int year = ChineseCalendar.GetYear(datetime);
            int month = ChineseCalendar.GetMonth(datetime);
            int day = ChineseCalendar.GetDayOfMonth(datetime);
            //获取闰月, 0 则表示没有闰月
            int leapMonth = ChineseCalendar.GetLeapMonth(year);

            bool isleap = false;

            if (leapMonth > 0)
            {
                if (leapMonth == month)
                {
                    //闰月
                    isleap = true;
                    month--;
                }
                else if (month > leapMonth)
                {
                    month--;
                }
            }

            return string.Concat(GetLunisolarYear(year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
        }




        #endregion

 

                接着,拼接好返回的字符:

              

  Console.Write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\r\n" + "农历为:"+GetChineseDateTime(DateTime.Now));



             为了测试外部调用的情况,我们可以这样,一个ajax过来,然后提交到handler里面,handler调用这些接口来测试下。

                   需要注意的是,

                       1,拼接好的字符串里面的换行符最好是HTML标签中的<br/>,不使用\n;

                       2,调用百度等大型开发平台的API接口的时候,对于返回的JSON,处理JSON转对象的时候,要注意JSON里面数组都要变成LIST,然后再调用自己构造的泛型方法去转换。

下面分享一些私人手藏的接口:

     

http://api.ajaxsns.com/

http://www.djdkx.com/open/randxml

http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak=这里填写自己的AK

http://www.twototwo.cn/train/QueryTrainScheduleByNumber.aspx

http://www.chepiao100.com/my/doc/checi.html

http://www.haoservice.com/apilist/

http://www.bejson.com/go.html?u=http://www.bejson.com/webInterface.html


        网上也有很多付费的接口,另外,今天做黄金查询的时候,发现了个聚合数据也不错哟~


    

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109523.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Matlab基本函数 length函数

    Matlab基本函数 length函数分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1、length函数:

    2022年6月9日
    70
  • Yarn 安装与使用详细介绍「建议收藏」

    Yarn 安装与使用详细介绍「建议收藏」背景什么是Yarn速度快离线模式可靠可确定性网络优化扁平化模式版本控制其他关于Yarn的介绍Yarn安装windowsmac方式一方式二Yarn换源背景在Node生态系统中,依赖通常安装在项目的node_modules文件夹中。然而,这个文件的结构和实际依赖树可能有所区别,因为重复的依赖可以合并到一起。npm客户端把依…

    2022年5月26日
    61
  • Python之struct

    1.功能(1)按照指定格式将Python数据转换为字符串(该字符串为字节流)(2)按照指定格式将字节流转换为Python指定的数据类型(3)处理二进制数据,如果用struct来处理文件的

    2021年12月18日
    56
  • Python 九九乘法表[通俗易懂]

    Python 九九乘法表[通俗易懂]#python九九乘法表#创建外层循环控制高度i=0whilei<9:#先+=,从1开始计算i+=1#创建内层循环控制宽度j=0whilej

    2022年7月5日
    24
  • 最小可用maven+springboot 项目(无法使用外网,但是有maven私库情况)

    最小可用maven+springboot 项目(无法使用外网,但是有maven私库情况)用的是ideal,jdk1.8具体操作,参考下面链接。致谢:感谢下面作者的博客https://blog.csdn.net/weixin_43293627/article/details/82877418https://blog.csdn.net/u011948899/article/details/78159027https://www.phpsong.com/3463.ht…

    2022年7月18日
    11
  • idea 集成svn_idea怎么关联svn

    idea 集成svn_idea怎么关联svn当你idea集成svn工具时:依照如下步骤:1.下载并安装svn:链接:https://pan.baidu.com/s/1jIs00Pg密码:2o3x2.特别说明:如果你没选上:那么你会在你的安装路径上无法找到svn.exe3.之后你就可以正常的使用svn。

    2022年10月17日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号