项目要从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"));
}
}
}
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"));
}
}
}