黑马程序员--用户输入一个“2008-01-02”格式的日期,分析用户输入的日期然后按照“2008年1月2日”的格式重新输出。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace test1 7 { 8 class Program 9 { 10 //1、 用户输入一个“2008-01-02”格式的日期,分析用户输入的日期然后按照“2008年1月2日”的格式重新输出。 11 static void Main(string[] args) 12 { 13 //提示用户输入日期的格式。 14 Console.WriteLine("请输入日期格式:例如:2008-01-02"); 15 16 //接收用户输入的值,赋给字符串变量 date 17 18 String date = Console.ReadLine(); 19 //分割字符串date分割符为“-”号,为把分割后的值赋给字符串数组dates 20 21 String[] dates = date.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries); 22 23 //处理月份和日期前面带0的字符串 24 int M = 0; 25 int d = 0; ; 26 try 27 { 28 M = Convert.ToInt32(dates[1]); 29 d = Convert.ToInt32(dates[2]); 30 } 31 catch (Exception e) 32 { 33 Console.WriteLine(e.ToString()); 34 return; 35 } 36 37 //输出新的日期格式为:t1[0]+“年”+ M+“月”+ d+“日” 38 Console.Write(String.Format("{0}年{1}月{2}日", dates[0], M, d)); 39 Console.ReadKey(); 40 41 } 42 } 43 }
[url=http://www.iliangdian.com]恩施门户网:城市亮点网[/url]