【代码保留】Quarter类

一组关于季度的类
可以实现时间比对(是否超时等的基本判断)(仅作保留,非公开)

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplicationQuarter
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
//第四季度
            Quarter qua = QuarterFactory.Create(4);
            qua.Year 
= 2007;
            qua.OvertimeDate 
= 10;
            Console.WriteLine(
"起始月份:" + qua.StartMonth);
            Console.WriteLine(
"结束月份:" + qua.EndMonth);
            DateTime date;
            
//临界值,跨年(第四季度)
            date = new DateTime(200819);
            date 
= new DateTime(2008110);
            date 
= new DateTime(2008111);
            
//临界值,跨年(第四季度)
            date = new DateTime(20071011);
            date 
= new DateTime(20071010);
            date 
= new DateTime(2007109);

            
//第二季度
            qua = QuarterFactory.Create(2);
            qua.Year 
= 2007;
            qua.OvertimeDate 
= 10;
            Console.WriteLine(
"起始月份:" + qua.StartMonth);
            Console.WriteLine(
"结束月份:" + qua.EndMonth);
            
//临界值
            date = new DateTime(200749);
            date 
= new DateTime(2007410);
            date 
= new DateTime(2007411);

            
//不同年份
            date = new DateTime(200849);
            date 
= new DateTime(2008410);
            date 
= new DateTime(2008411);

            
//第一季度
            qua = QuarterFactory.Create(1);
            qua.Year 
= 2007;
            qua.OvertimeDate 
= 10;
            Console.WriteLine(
"起始月份:" + qua.StartMonth);
            Console.WriteLine(
"结束月份:" + qua.EndMonth);
            
//临界值
            date = new DateTime(200749);
            date 
= new DateTime(2007410);
            date 
= new DateTime(2007411);

            
//不同年份
            date = new DateTime(200849);
            date 
= new DateTime(2008410);
            date 
= new DateTime(2008411);

            
if (qua.Contains(date))
            
{
                Console.WriteLine(
"Contains");
            }

            
else
            
{
                Console.WriteLine(
"No Contains");
            }

        }

    }


    
/// <summary>
    
/// 季度工厂
    
/// </summary>

    public class QuarterFactory
    
{
        
/// <summary>
        
/// 通过数字指定一个季度
        
/// </summary>
        
/// <param name="quarter"></param>
        
/// <returns></returns>

        public static Quarter Create(int quarter)
        
{
            
return Create((QuarterType)quarter);
        }

        
/// <summary>
        
/// 通过枚举指定一个季度
        
/// </summary>
        
/// <param name="quarter"></param>
        
/// <returns></returns>

        public static Quarter Create(QuarterType quarter)
        
{
            
switch (quarter)
            
{
                
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;
            }

        }

    }

    
/// <summary>
    
/// 季度
    
/// </summary>

    public class Quarter
    
{
        
private int year;
        
/// <summary>
        
/// 当前季度所在的年份
        
/// </summary>

        public int Year
        
{
            
set
            
{
                
this.year = value;
            }

        }


        
protected int overtimeDate;
        
/// <summary>
        
/// 当前季度的超时时间,比如设定每个季度的10号为超时时间,则第一季度将从1月11日起至4月10日(含)
        
/// </summary>

        public int OvertimeDate
        
{
            
set
            
{
                
this.overtimeDate = value;
            }

        }


        
protected MonthType startMonth;
        
/// <summary>
        
/// 获取或设置本季度的起始月
        
/// </summary>

        public MonthType StartMonth
        
{
            
get
            
{
                
return this.startMonth;
            }

        }


        
private MonthType endMonth;
        
/// <summary>
        
/// 获取本季度的结束月
        
/// </summary>

        public MonthType EndMonth
        
{
            
get
            
{
                endMonth 
= (MonthType)(this.startMonth.GetHashCode() + 3);
                
if (endMonth.GetHashCode() > MonthType.December.GetHashCode())
                    endMonth 
= (MonthType)(endMonth.GetHashCode() - 12);
                
return endMonth;
            }

        }


        
/// <summary>
        
/// 确认参数中的时间是否包含在当前季度中
        
/// </summary>
        
/// <param name="month"></param>
        
/// <returns></returns>

        public bool Contains(DateTime time)
        
{
            DateTime stdStartDate 
= new DateTime(this.year, this.startMonth.GetHashCode(), this.overtimeDate);
            DateTime stdEndDate;
            
if (this.startMonth.GetHashCode()<MonthType.October.GetHashCode())
            
{
                stdEndDate 
= new DateTime(this.year, this.EndMonth.GetHashCode(), this.overtimeDate);
            }

            
else
            
{
                stdEndDate 
= new DateTime(this.year + 1this.EndMonth.GetHashCode(), this.overtimeDate);
            }

            
if (time <= stdStartDate || time > stdEndDate)
            
{
                
return false;
            }

            
else
            
{
                
return true;
            }

        }

    }

    
/// <summary>
    
/// 第一季度
    
/// </summary>

    public sealed class FirstQuarter : Quarter
    
{
        
public FirstQuarter()
        
{
            
//设置第一季度的起始月为一月
            startMonth = MonthType.January;
        }

    }

    
/// <summary>
    
/// 第二季度
    
/// </summary>

    public sealed class SecondQuarter : Quarter
    
{
        
public SecondQuarter()
        
{
            
//设置第二季度的起始月为四月
            startMonth = MonthType.April;
        }

    }

    
/// <summary>
    
/// 第三季度
    
/// </summary>

    public sealed class ThirdQuarter : Quarter
    
{
        
public ThirdQuarter()
        
{
            
//设置第三季度的起始月为七月
            startMonth = MonthType.July;
        }

    }

    
/// <summary>
    
/// 第四季度
    
/// </summary>

    public sealed class FourthQuarter : Quarter
    
{
        
public FourthQuarter()
        
{
            
//设置第四季度的起始月为十月
            startMonth = MonthType.October;
        }

    }

    
/// <summary>
    
/// 月份枚举
    
/// </summary>

    public enum MonthType
    
{
        
/// <summary>
        
/// 1月
        
/// </summary>

        January = 1,
        
/// <summary>
        
/// 2月
        
/// </summary>

        February = 2,
        
/// <summary>
        
/// 3月
        
/// </summary>

        March = 3,
        
/// <summary>
        
/// 4月
        
/// </summary>

        April = 4,
        
/// <summary>
        
/// 5月
        
/// </summary>

        May = 5,
        
/// <summary>
        
/// 6月
        
/// </summary>

        June = 6,
        
/// <summary>
        
/// 7月
        
/// </summary>

        July = 7,
        
/// <summary>
        
/// 8月
        
/// </summary>

        August = 8,
        
/// <summary>
        
/// 9月
        
/// </summary>

        September = 9,
        
/// <summary>
        
/// 10月
        
/// </summary>

        October = 10,
        
/// <summary>
        
/// 11月
        
/// </summary>

        November = 11,
        
/// <summary>
        
/// 12月
        
/// </summary>

        December = 12
    }

    
/// <summary>
    
/// 季度枚举
    
/// </summary>

    public enum QuarterType
    
{
        
/// <summary>
        
/// 第一季度
        
/// </summary>

        First = 1,
        
/// <summary>
        
/// 第二季度
        
/// </summary>

        Second = 2,
        
/// <summary>
        
/// 第三季度
        
/// </summary>

        Third = 3,
        
/// <summary>
        
/// 第四季度
        
/// </summary>

        Fourth = 4
    }


}

另:
        /// <summary>
        
/// 季度
        
/// </summary>

        public class Quarter : IComparable<DateTime>
        
{
            
/// <summary>
            
/// 通过数字指定一个季度(1,2,3,4)
            
/// </summary>
            
/// <param name="quarter"></param>
            
/// <returns></returns>

            public Quarter(int quarter)
            
{
                
this.Initialization((QuarterType)quarter);
            }

            
/// <summary>
            
/// 通过枚举指定一个季度
            
/// </summary>
            
/// <param name="quarter"></param>

            public Quarter(QuarterType quarter)
            
{
                
this.Initialization(quarter);
            }

            
/// <summary>
            
/// 给Quarter对象赋初值
            
/// </summary>
            
/// <param name="quarter"></param>

            private void Initialization(QuarterType quarter)
            
{
                
switch (quarter)
                
{
                    
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;
                }

            }


            
private QuarterType value = QuarterType.First;
            
/// <summary>
            
/// 当前季度
            
/// </summary>

            public QuarterType Value
            
{
                
get
                
{
                    
return value;
                }

            }


            
private int year = 2000;
            
/// <summary>
            
/// 当前季度所在的年份
            
/// </summary>

            public int Year
            
{
                
get
                
{
                    
return this.year;
                }

                
set
                
{
                    
this.year = value;
                }

            }


            
protected int overtimeDate = 1;
            
/// <summary>
            
/// 当前季度的超时时间,比如设定每个季度的10号为超时时间,则第一季度将从1月11日起至4月10日(含)
            
/// </summary>

            public int OvertimeDate
            
{
                
set
                
{
                    
this.overtimeDate = value;
                }

            }


            
protected MonthType startMonth = MonthType.January;
            
/// <summary>
            
/// 本季度的起始月
            
/// </summary>

            public MonthType StartMonth
            
{
                
get
                
{
                    
return this.startMonth;
                }

            }


            
private MonthType endMonth;
            
/// <summary>
            
/// 本季度的结束月
            
/// </summary>

            public MonthType EndMonth
            
{
                
get
                
{
                    endMonth 
= (MonthType)(this.startMonth.GetHashCode() + 3);
                    
if (endMonth.GetHashCode() > MonthType.December.GetHashCode())
                        endMonth 
= (MonthType)(endMonth.GetHashCode() - 12);
                    
return endMonth;
                }

            }


            
/// <summary>
            
/// 确认参数中的时间是否包含在当前季度中
            
/// </summary>
            
/// <param name="month"></param>
            
/// <returns></returns>

            public bool Contains(DateTime time)
            
{
                
if (this.CompareTo(time) == 0)
                
{
                    
return true;
                }

                
else
                
{
                    
return false;
                }

            }



            
IComparable 成员
        }

原文地址:https://www.cnblogs.com/volnet/p/838686.html