获取规定法则的年月日

根据传入的日期,年份(整数),循环数(整数),及字符串数组等参数,返回三位数的“年月日"字符串;

比如,定义字符串数组string[] a=new string[31] 

{ "1","2","3","4","5","6","7","8","9",
  "A","B","C","D","E","F","G","H","J",
  "K","L","M","N","P","R","S","T","V",
  "W","X","Y","O"};

然后执行方法getYMD(DateTime.Prase("2021-10-22",2011,30,a),则返回"BBN"。

 

        /// <summary>
        /// 获取规定法则的年月日
        /// </summary>
        /// <param name="date">日期</param>
        /// <param name="start">开始计算的年份整数</param>
        /// <param name="recycle">轮回数(即多少年循环一次)</param>
        /// <param name="a">年份对应的编码数组</param>
        /// <returns></returns>
        public string getYMD(DateTime date,int start,int recycle,string[] a)
        {
            //string[] a = new string[31] { "1","2","3","4","5","6","7","8","9",
            //                              "A","B","C","D","E","F","G","H","J",
            //                              "K","L","M","N","P","R","S","T","V",
            //                              "W","X","Y","O"};
            string yyyyMMdd = date.ToString("yyyyMMdd");    //把日期转换为八位数的年月日字符串
            int y = int.Parse(yyyyMMdd.Substring(0, 4));    //取年份整数
            int m = int.Parse(yyyyMMdd.Substring(4, 2));    //取月份整数
            int d = int.Parse(yyyyMMdd.Substring(6));       //取日期整数

            string strY = a[(y-start) % recycle ];
            string strM = a[m - 1];
            string strD = a[d - 1];
            yyyyMMdd = strY + strM + strD;
            return yyyyMMdd;
        }

 

上一篇:java处理银行卡号格式(每四位添加空格)


下一篇:某个公司的机试题(一)