代码如下:
1
using
System;
2 using System.Text;
3 using System.Text.RegularExpressions;
4
5 namespace CSharpDateConvert
6 {
7 class DateConvert
8 {
9 private static DateConvert m_DateConvert = null;
10
11 private char[] strChinese;
12
13 private DateConvert()
14 {
15 strChinese = new char[] {
16 '〇','一','二','三','四','五','六','七','八','九','十'
17 };
18 }
19
20 public static DateConvert Instance
21 {
22 get
23 {
24 if (m_DateConvert == null)
25 m_DateConvert = new DateConvert();
26 return m_DateConvert;
27 }
28 }
29
30 public string Baodate2Chinese(string strDate)
31 {
32 StringBuilder result = new StringBuilder();
33
34 // 依据正则表达式判断参数是否正确
35 Regex theReg = new Regex(@"(\d{2}|\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})");
36 if (theReg.Match(strDate).Length != 0)
37 {
38 // 将数字日期的年月日存到字符数组str中
39 string[] str = null;
40 if (strDate.Contains("-"))
41 {
42 str = strDate.Split('-');
43 }
44 else if (strDate.Contains("/"))
45 {
46 str = strDate.Split('/');
47 }
48
49 // str[0]中为年,将其各个字符转换为相应的汉字
50 for (int i = 0; i < str[0].Length; i++)
51 {
52 result.Append(strChinese[int.Parse(str[0][i].ToString())]);
53 }
54 result.Append("年");
55
56 // 转换月
57 int month = int.Parse(str[1]);
58 int MN1 = month / 10;
59 int MN2 = month % 10;
60
61 if (MN1 > 1)
62 {
63 result.Append(strChinese[MN1]);
64 }
65 if (MN1 > 0)
66 {
67 result.Append(strChinese[10]);
68 }
69 if (MN2 != 0)
70 {
71 result.Append(strChinese[MN2]);
72 }
73 result.Append("月");
74
75 // 转换日
76 int day = int.Parse(str[2]);
77 int DN1 = day / 10;
78 int DN2 = day % 10;
79
80 if (DN1 > 1)
81 {
82 result.Append(strChinese[DN1]);
83 }
84 if (DN1 > 0)
85 {
86 result.Append(strChinese[10]);
87 }
88 if (DN2 != 0)
89 {
90 result.Append(strChinese[DN2]);
91 }
92 result.Append("日");
93 }
94 else
95 {
96 throw new ArgumentException();
97 }
98
99 return result.ToString();
100 }
101
102 static void Main(string[] args)
103 {
104 Console.WriteLine(DateConvert.Instance.Baodate2Chinese("2005-10-10"));
105 Console.WriteLine(DateConvert.Instance.Baodate2Chinese("05-12-31"));
106 Console.WriteLine(DateConvert.Instance.Baodate2Chinese("2005/10/10"));
107 Console.WriteLine(DateConvert.Instance.Baodate2Chinese("05/12/31"));
108 }
109 }
110}
111
2 using System.Text;
3 using System.Text.RegularExpressions;
4
5 namespace CSharpDateConvert
6 {
7 class DateConvert
8 {
9 private static DateConvert m_DateConvert = null;
10
11 private char[] strChinese;
12
13 private DateConvert()
14 {
15 strChinese = new char[] {
16 '〇','一','二','三','四','五','六','七','八','九','十'
17 };
18 }
19
20 public static DateConvert Instance
21 {
22 get
23 {
24 if (m_DateConvert == null)
25 m_DateConvert = new DateConvert();
26 return m_DateConvert;
27 }
28 }
29
30 public string Baodate2Chinese(string strDate)
31 {
32 StringBuilder result = new StringBuilder();
33
34 // 依据正则表达式判断参数是否正确
35 Regex theReg = new Regex(@"(\d{2}|\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})");
36 if (theReg.Match(strDate).Length != 0)
37 {
38 // 将数字日期的年月日存到字符数组str中
39 string[] str = null;
40 if (strDate.Contains("-"))
41 {
42 str = strDate.Split('-');
43 }
44 else if (strDate.Contains("/"))
45 {
46 str = strDate.Split('/');
47 }
48
49 // str[0]中为年,将其各个字符转换为相应的汉字
50 for (int i = 0; i < str[0].Length; i++)
51 {
52 result.Append(strChinese[int.Parse(str[0][i].ToString())]);
53 }
54 result.Append("年");
55
56 // 转换月
57 int month = int.Parse(str[1]);
58 int MN1 = month / 10;
59 int MN2 = month % 10;
60
61 if (MN1 > 1)
62 {
63 result.Append(strChinese[MN1]);
64 }
65 if (MN1 > 0)
66 {
67 result.Append(strChinese[10]);
68 }
69 if (MN2 != 0)
70 {
71 result.Append(strChinese[MN2]);
72 }
73 result.Append("月");
74
75 // 转换日
76 int day = int.Parse(str[2]);
77 int DN1 = day / 10;
78 int DN2 = day % 10;
79
80 if (DN1 > 1)
81 {
82 result.Append(strChinese[DN1]);
83 }
84 if (DN1 > 0)
85 {
86 result.Append(strChinese[10]);
87 }
88 if (DN2 != 0)
89 {
90 result.Append(strChinese[DN2]);
91 }
92 result.Append("日");
93 }
94 else
95 {
96 throw new ArgumentException();
97 }
98
99 return result.ToString();
100 }
101
102 static void Main(string[] args)
103 {
104 Console.WriteLine(DateConvert.Instance.Baodate2Chinese("2005-10-10"));
105 Console.WriteLine(DateConvert.Instance.Baodate2Chinese("05-12-31"));
106 Console.WriteLine(DateConvert.Instance.Baodate2Chinese("2005/10/10"));
107 Console.WriteLine(DateConvert.Instance.Baodate2Chinese("05/12/31"));
108 }
109 }
110}
111