一组关于季度的类
可以实现时间比对(是否超时等的基本判断)(仅作保留,非公开)
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace ConsoleApplicationQuarter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//第四季度
Quarter qua = QuarterFactory.Create(4);
qua.Year = 2007;
qua.OvertimeDate = 10;
Console.WriteLine("起始月份:" + qua.StartMonth);
Console.WriteLine("结束月份:" + qua.EndMonth);
DateTime date;
//临界值,跨年(第四季度)
date = new DateTime(2008, 1, 9);
date = new DateTime(2008, 1, 10);
date = new DateTime(2008, 1, 11);
//临界值,跨年(第四季度)
date = new DateTime(2007, 10, 11);
date = new DateTime(2007, 10, 10);
date = new DateTime(2007, 10, 9);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//第二季度
qua = QuarterFactory.Create(2);
qua.Year = 2007;
qua.OvertimeDate = 10;
Console.WriteLine("起始月份:" + qua.StartMonth);
Console.WriteLine("结束月份:" + qua.EndMonth);
//临界值
date = new DateTime(2007, 4, 9);
date = new DateTime(2007, 4, 10);
date = new DateTime(2007, 4, 11);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//不同年份
date = new DateTime(2008, 4, 9);
date = new DateTime(2008, 4, 10);
date = new DateTime(2008, 4, 11);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//第一季度
qua = QuarterFactory.Create(1);
qua.Year = 2007;
qua.OvertimeDate = 10;
Console.WriteLine("起始月份:" + qua.StartMonth);
Console.WriteLine("结束月份:" + qua.EndMonth);
//临界值
date = new DateTime(2007, 4, 9);
date = new DateTime(2007, 4, 10);
date = new DateTime(2007, 4, 11);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//不同年份
date = new DateTime(2008, 4, 9);
date = new DateTime(2008, 4, 10);
date = new DateTime(2008, 4, 11);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (qua.Contains(date))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("Contains");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("No Contains");
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 季度工厂
/// </summary>
public class QuarterFactory
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 通过数字指定一个季度
/// </summary>
/// <param name="quarter"></param>
/// <returns></returns>
public static Quarter Create(int quarter)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Create((QuarterType)quarter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 通过枚举指定一个季度
/// </summary>
/// <param name="quarter"></param>
/// <returns></returns>
public static Quarter Create(QuarterType quarter)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (quarter)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case QuarterType.First:
return new FirstQuarter();
case QuarterType.Second:
return new SecondQuarter();
case QuarterType.Third:
return new ThirdQuarter();
case QuarterType.Fourth:
return new FourthQuarter();
default:
return null;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 季度
/// </summary>
public class Quarter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private int year;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 当前季度所在的年份
/// </summary>
public int Year
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.year = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected int overtimeDate;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 当前季度的超时时间,比如设定每个季度的10号为超时时间,则第一季度将从1月11日起至4月10日(含)
/// </summary>
public int OvertimeDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.overtimeDate = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected MonthType startMonth;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取或设置本季度的起始月
/// </summary>
public MonthType StartMonth
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.startMonth;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private MonthType endMonth;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取本季度的结束月
/// </summary>
public MonthType EndMonth
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
endMonth = (MonthType)(this.startMonth.GetHashCode() + 3);
if (endMonth.GetHashCode() > MonthType.December.GetHashCode())
endMonth = (MonthType)(endMonth.GetHashCode() - 12);
return endMonth;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 确认参数中的时间是否包含在当前季度中
/// </summary>
/// <param name="month"></param>
/// <returns></returns>
public bool Contains(DateTime time)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DateTime stdStartDate = new DateTime(this.year, this.startMonth.GetHashCode(), this.overtimeDate);
DateTime stdEndDate;
if (this.startMonth.GetHashCode()<MonthType.October.GetHashCode())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
stdEndDate = new DateTime(this.year, this.EndMonth.GetHashCode(), this.overtimeDate);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
stdEndDate = new DateTime(this.year + 1, this.EndMonth.GetHashCode(), this.overtimeDate);
}
if (time <= stdStartDate || time > stdEndDate)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 第一季度
/// </summary>
public sealed class FirstQuarter : Quarter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public FirstQuarter()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//设置第一季度的起始月为一月
startMonth = MonthType.January;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 第二季度
/// </summary>
public sealed class SecondQuarter : Quarter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public SecondQuarter()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//设置第二季度的起始月为四月
startMonth = MonthType.April;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 第三季度
/// </summary>
public sealed class ThirdQuarter : Quarter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public ThirdQuarter()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//设置第三季度的起始月为七月
startMonth = MonthType.July;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 第四季度
/// </summary>
public sealed class FourthQuarter : Quarter
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public FourthQuarter()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//设置第四季度的起始月为十月
startMonth = MonthType.October;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 月份枚举
/// </summary>
public enum MonthType
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 1月
/// </summary>
January = 1,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 2月
/// </summary>
February = 2,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 3月
/// </summary>
March = 3,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 4月
/// </summary>
April = 4,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 5月
/// </summary>
May = 5,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 6月
/// </summary>
June = 6,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 7月
/// </summary>
July = 7,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 8月
/// </summary>
August = 8,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 9月
/// </summary>
September = 9,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 10月
/// </summary>
October = 10,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 11月
/// </summary>
November = 11,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 12月
/// </summary>
December = 12
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 季度枚举
/// </summary>
public enum QuarterType
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 第一季度
/// </summary>
First = 1,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 第二季度
/// </summary>
Second = 2,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 第三季度
/// </summary>
Third = 3,
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 第四季度
/// </summary>
Fourth = 4
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
另:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 季度
/// </summary>
public class Quarter : IComparable<DateTime>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 通过数字指定一个季度(1,2,3,4)
/// </summary>
/// <param name="quarter"></param>
/// <returns></returns>
public Quarter(int quarter)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Initialization((QuarterType)quarter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 通过枚举指定一个季度
/// </summary>
/// <param name="quarter"></param>
public Quarter(QuarterType quarter)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Initialization(quarter);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 给Quarter对象赋初值
/// </summary>
/// <param name="quarter"></param>
private void Initialization(QuarterType quarter)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (quarter)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case QuarterType.First:
//设置第一季度的起始月为一月
startMonth = MonthType.January;
this.value = QuarterType.First;
break;
case QuarterType.Second:
//设置第二季度的起始月为四月
startMonth = MonthType.April;
this.value = QuarterType.Second;
break;
case QuarterType.Third:
//设置第三季度的起始月为七月
startMonth = MonthType.July;
this.value = QuarterType.Third;
break;
default://case QuarterType.Fourth:
//设置第四季度的起始月为十月
startMonth = MonthType.October;
this.value = QuarterType.Fourth;
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private QuarterType value = QuarterType.First;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 当前季度
/// </summary>
public QuarterType Value
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int year = 2000;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 当前季度所在的年份
/// </summary>
public int Year
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.year;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.year = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected int overtimeDate = 1;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 当前季度的超时时间,比如设定每个季度的10号为超时时间,则第一季度将从1月11日起至4月10日(含)
/// </summary>
public int OvertimeDate
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.overtimeDate = value;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected MonthType startMonth = MonthType.January;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 本季度的起始月
/// </summary>
public MonthType StartMonth
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.startMonth;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private MonthType endMonth;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 本季度的结束月
/// </summary>
public MonthType EndMonth
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
endMonth = (MonthType)(this.startMonth.GetHashCode() + 3);
if (endMonth.GetHashCode() > MonthType.December.GetHashCode())
endMonth = (MonthType)(endMonth.GetHashCode() - 12);
return endMonth;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 确认参数中的时间是否包含在当前季度中
/// </summary>
/// <param name="month"></param>
/// <returns></returns>
public bool Contains(DateTime time)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.CompareTo(time) == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
IComparable 成员#region IComparable<DateTime> 成员
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 当前季度与时间的比较
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(DateTime other)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DateTime stdStartDate = new DateTime(this.year, this.startMonth.GetHashCode(), this.overtimeDate);
DateTime stdEndDate;
if (this.startMonth.GetHashCode() < MonthType.October.GetHashCode())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
stdEndDate = new DateTime(this.year, this.EndMonth.GetHashCode(), this.overtimeDate);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
stdEndDate = new DateTime(this.year + 1, this.EndMonth.GetHashCode(), this.overtimeDate);
}
if (stdStartDate >= other)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//大于
return 1;
}
else if (stdEndDate < other)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//小于
return -1;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//等于
return 0;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)