C # convert numeric date to Chinese lowercase date

Source: Internet
Author: User
According to customer requirements, convert "-11-4""

Using system;
Using system. text;
Using
System. Text. regularexpressions;

Namespace andydateconvert
{

Class dateconvert
{
Private Static dateconvert
M_dateconvert = NULL;

Private char [] strchinese;


Private dateconvert ()
{
Strchinese = new char []
{

'0', 'yi', '2', '3', '4', '5', '6', '7', '8', '9 ', '10'
};

}

Public static dateconvert instance

{
Get
{
If (m_dateconvert =
Null)
M_dateconvert = new
Dateconvert ();
Return m_dateconvert;

}
}

Public String baodate2chinese (string
Strdate)
{
Stringbuilder result = new
Stringbuilder ();

// Judge whether the parameter is correct based on the regular expression
RegEx
Thereg = new RegEx (@ "(D {2} | D {4}) (/|-) (d {1, 2}) (/|-) (d {1, 2 }) ");
If
(Thereg. Match (strdate). length! = 0)
{
//
Save the year, month, and day of the digit date to the character array 'str '.
String [] STR = NULL;

If (strdate. Contains ("-"))
{
STR =
Strdate. Split ('-');
}
Else if
(Strdate. Contains ("/"))
{
STR =
Strdate. Split ('/');
}

//
STR [0] is a year, which converts each character into a corresponding Chinese Character
For (INT I = 0; I <
STR [0]. length; I ++)
{

Result. append (strchinese [Int. parse (STR [0] [I]. tostring ()]);

}
Result. append ("year ");

//
Conversion month
Int month = int. parse (STR [1]);
Int
Mn1 = month/10;
Int Fe2 = month %
10;

If (Mn1> 1)

{
Result. append (strchinese [Mn1]);

}
If (Mn1> 0)

{
Result. append (strchinese [10]);

}
If (Fe2! = 0)

{
Result. append (strchinese [Fe2]);

}
Result. append ("month ");

//
Conversion date
Int day = int. parse (STR [2]);
Int dn1
= Day/10;
Int dn2 = day % 10;

If
(Dn1> 1)
{

Result. append (strchinese [dn1]);
}
If
(Dn1> 0)
{

Result. append (strchinese [10]);
}
If (dn2
! = 0)
{

Result. append (strchinese [dn2]);
}

Result. append ("day ");
}
Else

{
Throw new argumentexception ();

}

Return result. tostring ();
}

Static void main (string [] ARGs)
{

Console. writeline (dateconvert. instance. baodate2chinese ("2007-11-4 "));

Console. writeline (dateconvert. instance. baodate2chinese ("07-11-4 "));

Console. writeline (dateconvert. instance. baodate2chinese ("2007/11/4 "));

Console. writeline (dateconvert. instance. baodate2chinese ("07/11/4 "));

}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.