CharlesChen's Technical Space

简单实用是我一直在软件开发追求的目标(I Focus on. Net technology, to make the greatest efforts to enjoy the best of life.)
Not the best, only better
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

移动项目开发笔记(C#数字日期转换成中文日期(zt))

Posted on 2008-09-15 00:43  Charles Chen  阅读(830)  评论(0编辑  收藏  举报

     项目要从OA公文系统系统做接口开发。需要把项目中的数据生成公文文档,日期是一个需要转换的问题,如何把一个日期类型用中文表示。

如:"2008-09-15"转换成"二〇〇八年九月十五日".

通过在网上查找,找到一个不错的算法。先分享出来给朋友们:感谢原作者无私的奉献和辛勤的工作成果。谢谢!

原创作者http://blog.csdn.net/qq317942417/archive/2008/06/06/2518124.aspx

下面是代码展示:

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ConvertDateToChinese
{
    
class Convert
    {
        
private static Convert instance=null;
        
public static Convert Instance
        {
            
get
            {
                
if (instance == null)
                {
                    instance 
= new Convert();
                   
                }
                
return instance;
            }

        }

        
public char[] chinese;
        
private Convert()
        {
            chinese
=new char[]{'0','','','','','','','','','',''};
        }


        
//Method for Convert to Chinese
        public string ConvertMethod(string date)
        {
            
// Define stringBuilder variable  for write out the chinese of date
            StringBuilder strb = new StringBuilder();

            
//define regex regularExpressins variable
            Regex regex = new Regex(@"(d{2}|d{4})(-|/)(d{1}|d{2})(-|/)(d{1}|d{2})");
            
if (!regex.IsMatch(date) == true)
            {
             
string[] str=null;
             
if (date.Contains("-"))
             {
                 str 
= date.Split('-');
             }
             
else
             {
                 
if(date.Contains("/"))
                 {
                     str
=date.Split('/');

                 }
             }
                            
//Convert year as follow
             for (int i = 0; i < str[0].Length; i++)
             {
                 strb 
= strb.Append(chinese[int.Parse(str[0][i].ToString())]);
             }
             strb.Append(
"");
              
                  
//Convert month as follow
            
// if (chinese[int.Parse(str[1][0].ToString())] != '0')
            
// {
            
//     strb.Append(chinese[int.Parse(str[1][0].ToString())]);
            
// }
            
// for (int i = 1; i < str[1].Length; i++)
            
// {
            
//     strb = strb.Append(chinese[int.Parse(str[1][i].ToString())]);
            
// }
            
// strb.Append("月");

            
//        //Convert day as follow
            
// if (chinese[int.Parse(str[2][0].ToString())] !='0')
            
// {
            
//     strb.Append(chinese[int.Parse(str[2][0].ToString())]);
            
// }
            
// for (int i = 1; i < str[2].Length; i++)
            
// {
            
//     strb = strb.Append(chinese[int.Parse(str[2][i].ToString())]);
            
// }
             
// strb.Append("日");
             #region  //convert month
             
int monthod = int.Parse(str[1]);
                
int MN1 = monthod / 10;
                
int MN2 = monthod % 10;


                
if (MN1 > 1)
                {
                    strb.Append(chinese[MN1]);
                }
                
if (MN1 > 0)
                {
                    strb.Append(chinese[
10]);
                }
                
if (MN2 != 0)
                {
                    strb.Append(chinese[MN2]);
                    strb.Append(
"");
                }
             
#endregion
             
#region //convert day
                
int day = int.Parse(str[2]);
                
int day1 = day / 10;
                
int day2 = day % 10;

                
if (day1 > 1)
                {
                    strb.Append(chinese[day1]);
                   
                    
                }
                
if (day1 > 0)
                {
                    strb.Append(chinese[
10]);
                }
                
if (day2 > 0)
                {
                    strb.Append(chinese[day2]);
                    strb.Append(
"");
                }
 
                
#endregion


            }
               
            
else
            {
                
throw new ArgumentException();
               
            }
            
return strb.ToString();


            

        }
        
static void Main(string[] args)
        {
          Console.WriteLine(Convert.Instance.ConvertMethod(
"08-09-15"));
        }
    }
}