📄 sample61.cs
字号:
namespace apiBook
{
using System;
using System.Globalization;
public class TestGregorianCalendarClass
{
public static void Main()
{
GregorianCalendar testG=new GregorianCalendar(GregorianCalendarTypes.Localized);
//使用带参构造函数初始化对象
Console.WriteLine("使用的公历版本:"+testG.CalendarType);
Console.Write("今天是"+testG.GetEra(DateTime.Now)+"纪元");
//使用GetEra方法获取纪元号
Console.Write(testG.GetYear(DateTime.Now)+"年");
//使用GetYear方法获取年份信息
Console.Write(testG.GetMonth(DateTime.Now)+"月");
//使用GetMonth方法获取月份信息
Console.Write(testG.GetDayOfMonth(DateTime.Now)+"号");
//使用GetDayOfMonth方法获取该DateTime对象是该月的第几号
Console.WriteLine("今天是星期"+testG.GetDayOfWeek(DateTime.Now));
//使用GetDayOfWeek方法获取该DateTime对象是星期几
Console.Write("现在是:"+testG.GetHour(DateTime.Now)+"点");
Console.Write(testG.GetMinute(DateTime.Now)+"分");
Console.Write(testG.GetSecond(DateTime.Now)+"秒");
Console.WriteLine(testG.GetMilliseconds(DateTime.Now)+"毫秒");
Console.WriteLine("今天是今年的第"+testG.GetDayOfYear(DateTime.Now)+"天");
//使用GetDayOfYear方法获取该DateTime对象是当前第几天的信息
Console.WriteLine("本周是今年第"+testG.GetWeekOfYear(DateTime.Now,CalendarWeekRule.FirstDay,DayOfWeek.Sunday)+"周");
//使用GetWeekOfYear方法
Console.WriteLine("本月是今年第"+testG.GetMonthsInYear(DateTime.Now.Year)+"个月");
//使用GetMonthsInYear方法获取月份信息
Console.WriteLine("当前日期加3天是:"+testG.AddDays(DateTime.Now,3));
//使用AddDays方法获取在当前DateTime对象的值上加3天的DateTime对象
Console.WriteLine("当前日期加2周是:"+testG.AddWeeks(DateTime.Now,2));
//使用AddWeeks方法获取在当前DateTime对象的值上加2周的DateTime对象
Console.WriteLine("当前日期加4个月是:"+testG.AddMonths(DateTime.Now,4));
//使用AddMonths方法获取在当前DateTime对象的值上加4个月的DateTime对象
Console.WriteLine("当前日期加2年是:"+testG.AddYears(DateTime.Now,2));
//使用AddYears方法获取在当前DateTime对象的值上加2年的DateTime对象
Console.Write("使用ToDateTime方法生成下面DateTime对象:");
DateTime testD=testG.ToDateTime(2004,2,28,2,2,2,2,1);
//使用ToDateTime方法获取DateTime对象
Console.WriteLine(testD.ToString());
Console.WriteLine("当前的08代表"+testG.ToFourDigitYear(08)+"年");
//使用ToFourDigitYear方法将2位数的年份信息转换为4位整数
Console.WriteLine("今年是否是闰年?"+testG.IsLeapYear(DateTime.Now.Year));
//使用IsLeapYear方法判断当前DateTime对象是否是闰年
Console.WriteLine("本月是否是闰月?"+testG.IsLeapMonth(DateTime.Now.Year,DateTime.Now.Month));
//使用IsLeapMonth方法判断当前DateTime对象是否是闰月
Console.WriteLine("今天是否是闰日"+testG.IsLeapDay(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day));
//使用IsLeapDay方法判断当前DateTime对象是否是闰日
Console.WriteLine("2004年2月29日是否是闰日"+testG.IsLeapDay(2004,2,29));
Console.WriteLine("2004年2月份有"+testG.GetDaysInMonth(2004,2)+"天");
//使用GetDaysInMonth方法获取某年某月的天数信息
Console.WriteLine("2004年有"+testG.GetDaysInYear(2004)+"天");
//使用GetDaysInYear方法获取某年的天数信息
Console.ReadLine();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -