Format requirements:
Copy codeCode: SU MO TU WE TH FR SA
01 02 03 04
05 06 07 08 09 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
Code:Copy codeThe Code is as follows: class Interview1
{
Static void Main ()
{
PrintCalender (2011, 10 );
}
Public static void PrintCalender (int year, int month)
{
FormatDate fd = new formatDate (year, month );
String calender =
@ "Su mo tu we th fr Sa
{0} {0} {0} {0} {0} {0} {0} {0}
{0} {0} {0} {0} {0} {0} {0} {0}
{0} {0} {0} {0} {0} {0} {0} {0}
{0} {0} {0} {0} {0} {0} {0} {0}
{0} {0} {0} {0} {0} {0} {0} {0 }";
Calender = string. Format (calender, fd). TrimEnd ();
Console. WriteLine (calender );
}
}
Class formatDate: IFormattable
{
Int num;
Int max;
Public formatDate (int year, int month)
{
DateTime dt = new DateTime (year, month, 1 );
Num = (int) dt. DayOfWeek *-1;
Max = DateTime. DaysInMonth (year, month );
}
Public string ToString (string format, IFormatProvider formatProvider)
{
Return num ++ <0 | num> max? "": Num. ToString ("00 ");
}
}