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

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

             

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

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

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

            最近两天开始做的服务有,天气查询,日历,快递,火车,黄金。。。等六个服务做成接口,今天要分析的是这里面唯一没有调用外部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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 理解 Spring ApplicationListener

    理解 Spring ApplicationListener      ApplicationContext事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件处理。 如果容器中有一个ApplicationListenerBean,每当ApplicationContext发布ApplicationEvent时,ApplicationListen…

    2025年7月27日
    1
  • c#Parallel.ForEach控制线程数量

    c#Parallel.ForEach控制线程数量List<int>listI=newList<int>();for(inti=0;i<1000;i++){listI.Add(i);}ParallelOptionsoptions=newParallelOptions();option..

    2022年7月19日
    14
  • asp判断session是否为空

    asp判断session是否为空1IfSession(“sesName”)=””Then…2IfSession(“sesName”)=EmptyThen…3IfIsEmpty(Session(“sesName”))Then…4IfCint(Session(“sesName”))=0Then… 来自:百度空间

    2022年7月15日
    17
  • QTcpSocket 内存问题「建议收藏」

    QTcpSocket 内存问题「建议收藏」我自己测试也发现反复的connectToHost会有内存泄露,建议谨慎的使用!////////////////////////////////////////////////QTcpSocket类的方法connectToHost会泄露内存,即使把调用这个方法的QTcpSocket实例delete掉,内存也不会释放!反复connectToHost会导致段错误,十分危险。必须控制connectToH…

    2025年10月11日
    4
  • github最受欢迎语言(android开源框架)

    内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新ViewPager图表(Chart)菜单(Menu)浮动菜单对话框空白页滑动删除手势操作RecyclerViewCardColorDrawableSpinner布局模糊效果TabBarAppBar选择器(Picker)跑马灯日历时间主题样式ImageView通知聊天视图Head

    2022年4月13日
    53
  • linux keypad driver

    linux keypad driverDTS文件、driver文件

    2022年5月1日
    73

发表回复

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

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