趣味编程:打印日历
2015-10-29 13:39
330 查看
趣味编程:打印日历
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
namespace ConsoleApplication2
{
static class StringExtensions
{
public static string CenterString(this string stringToCenter, int totalLength)
{
return stringToCenter.PadLeft(((totalLength - stringToCenter.Length) / 2) + stringToCenter.Length)
.PadRight(totalLength);
}
}
class Program
{
static void Main(string[] args)
{
var year = DateTime.Now.Year;
var dts = new List<DateTime>();
for (DateTime dt = new DateTime(year, 1, 1), dt_end = new DateTime(year + 1, 1, 1); dt != dt_end; dt = dt.AddDays(1))
dts.Add(dt);
var grps = dts.GroupBy(dt => dt.Month,
(k, g) => g.GroupBy(dt => CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Sunday),
(k2, g2) => g2.ToList()).ToList()).ToList();
grps.ForEach(g => {
Console.WriteLine(g[0][0].ToString("Y").CenterString(21));
g.ForEach(g2 =>
Console.WriteLine(g2[0].Day == 1 ? "{0,21}" : "{0,-21}", string.Join("", g2.Select(dt => string.Format("{0,3}", dt.Day))))
);
});
}
}
}
/*
2015年1月
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
2015年2月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
...
...
...
2015年12月
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
namespace ConsoleApplication2
{
static class StringExtensions
{
public static string CenterString(this string stringToCenter, int totalLength)
{
return stringToCenter.PadLeft(((totalLength - stringToCenter.Length) / 2) + stringToCenter.Length)
.PadRight(totalLength);
}
}
class Program
{
static void Main(string[] args)
{
var year = DateTime.Now.Year;
var dts = new List<DateTime>();
for (DateTime dt = new DateTime(year, 1, 1), dt_end = new DateTime(year + 1, 1, 1); dt != dt_end; dt = dt.AddDays(1))
dts.Add(dt);
var grps = dts.GroupBy(dt => dt.Month,
(k, g) => g.GroupBy(dt => CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Sunday),
(k2, g2) => g2.ToList()).ToList()).ToList();
grps.ForEach(g => {
Console.WriteLine(g[0][0].ToString("Y").CenterString(21));
g.ForEach(g2 =>
Console.WriteLine(g2[0].Day == 1 ? "{0,21}" : "{0,-21}", string.Join("", g2.Select(dt => string.Format("{0,3}", dt.Day))))
);
});
}
}
}
/*
2015年1月
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
2015年2月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
...
...
...
2015年12月
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
*/
相关文章推荐
- 如何写出好的Java代码
- php新手需要注意的高效率编程
- 黄聪:C#超级延时方法,延迟系统时间但系统又能同时能执行其它任务
- 字符串问题归类--C和C++
- java 获取系统时间
- C#冒泡排序
- 1018. 锤子剪刀布 (20)
- 个人笔记 asp 04 循环读取数据库并用表格显示
- Java的引用类型
- Eclipse中项目的导入和导出
- Java的异常体系
- C# 关于用7zip压缩文件提示win32exception 系统找不到文件解决方案(win7 x64)
- Java内存分配
- Java中的内存泄露问题
- java核心技术细语解读易忽视点(一)(1-3章)
- Gomoku五子棋OC实现
- php分页
- C++开源矩阵计算工具——Eigen 在VS2005中的下载、配置与使用
- C++模板之隐式实例化、显示实例化、隐式调用、显示调用和模板特化详解
- C++模板之隐式实例化、显示实例化、隐式调用、显示调用和模板特化详解