blogjava's web log

          blogjava's web log
          ...

          c sharp DateUtility

          ???? public ? static ? class ?DateUtility
          ????{
          ????????
          /// ? <summary>
          ????????
          /// ?Returns?the?first?day?of?the?year?for?a?given?date
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="date"></param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ?DateTime?BeginOfYear(DateTime?date)
          ????????{
          ????????????
          return ? new ?DateTime(date.Year,? 1 ,? 1 );
          ????????}

          ????????
          /// ? <summary>
          ????????
          /// ?Returns?the?last?day?of?the?year?for?a?given?date
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="date"></param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ?DateTime?EndOfYear(DateTime?date)
          ????????{
          ????????????
          return ? new ?DateTime(date.Year,? 12 ,? 31 );
          ????????}

          ????????
          /// ? <summary>
          ????????
          /// ?Returns?the?first?day?of?a?month?for?a?given?date
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="date"></param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ?DateTime?BeginOfMonth(DateTime?date)
          ????????{
          ????????????
          return ? new ?DateTime(date.Year,?date.Month,? 1 );
          ????????}

          ????????
          /// ? <summary>
          ????????
          /// ?Returns?the?last?day?of?a?month?for?a?given?date
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="date"> Date </param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ?DateTime?EndOfMonth(DateTime?date)
          ????????{
          ????????????
          int ?year? = ?date.Year;
          ????????????
          int ?month? = ?date.Month;
          ????????????
          if ?(month? == ? 12 )
          ????????????{
          ????????????????year
          ++ ;
          ????????????????month
          = 1 ;
          ????????????}
          ????????????
          else
          ????????????????month
          ++ ;
          ????????????
          return ? new ?DateTime(year,month, 1 ).AddDays( - 1 );
          ????????}


          ????????
          /// ? <summary>
          ????????
          /// ?Returns?the?first?day?of?a?quarter
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="date"></param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ?DateTime?BeginOfQuarter(DateTime?date)
          ????????{
          ????????????
          int ?month? = ?(((date.Month? - 1 )? / ? 3 ) * 3 ) + 1 ;
          ????????????
          return ? new ?DateTime(date.Year,?month,? 1 );
          ????????}

          ????????
          /// ? <summary>
          ????????
          /// ?Returns?the?last?day?of?a?quarter
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="date"> Date </param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ?DateTime?EndOfQuarter(DateTime?date)
          ????????{
          ????????????
          return ?BeginOfQuarter(BeginOfMonth(date).AddMonths( + 3 )).AddDays( - 1 );
          ????????}

          ????????
          /// ? <summary>
          ????????
          /// ?Check?if?the?date?is?in?a?leapyear
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="date"> Date </param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ? bool ?LeapYear(DateTime?date)
          ????????{
          ????????????
          int ?year? = ?date.Year;
          ????????????
          return ?(year? % ? 4 ? == ? 0 )? && ?((year? % ? 100 ? != ? 0 )? || ?(year? % ? 400 ? == ? 0 ));
          ????????}

          ????????
          /// ? <summary>
          ????????
          /// ?Caclulates?the?weeknumber?for?a?given?date
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="date"> Date </param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ? int ?WeekOfYear(DateTime?date)
          ????????{????????????
          ????????????
          int []?t1? = ? new ? int [ 7 ]?{ - 1 ,?? 0 ,?? 1 ,?? 2 ,?? 3 ,? - 3 ,? - 2 };
          ????????????
          int []?t2? = ? new ? int [ 7 ]?{ - 4 ,?? 2 ,?? 1 ,?? 0 ,? - 1 ,? - 2 ,? - 3 };

          ????????????DateTime?newYear?
          = ?BeginOfYear(date);
          ????????????
          int ?doy1? = ?date.DayOfYear? + ?t1[( int )newYear.DayOfWeek];
          ????????????
          int ?doy2? = ?date.DayOfYear? + ?t2[( int )date.DayOfWeek];

          ????????????
          if ?(doy1? <= ? 0 )?
          ????????????????
          return ?WeekOfYear(newYear.AddDays( - 1 ));
          ????????????
          else
          ????????????????
          if ?(doy2? >= ?EndOfYear(newYear).DayOfYear?)
          ????????????????????
          return ? 1 ;
          ????????????????
          else
          ????????????????????
          return ?(doy1 - 1 ) / 7 ? + 1 ;
          ????????}

          ????????
          /// ? <summary>
          ????????
          /// ?Determines?how?much?days?a?month?has
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="date"> Date </param>
          ????????
          /// ? <returns> Number?of?days?in?the?month </returns>
          ???????? public ? static ? int ?DaysInMonth(DateTime?date)
          ????????{
          ????????????
          short []?daysPerMonth? = ? new ? short [ 12 ]?{? 31 ,? 28 ,? 31 ,? 30 ,? 31 ,? 30 ,? 31 ,? 31 ,? 30 ,? 31 ,? 30 ,? 31 ?};
          ????????????
          int ?month? = ?date.Month;
          ????????????
          if ?(month? == ? 2 ? && ?LeapYear(date))
          ????????????????
          return ?daysPerMonth[month? - ? 1 ]? + ? 1 ;
          ????????????
          else
          ????????????????
          return ?daysPerMonth[month? - ? 1 ];
          ????????}


          ????????
          /// ? <summary>
          ????????
          /// ?Return?the?duration?between?two?dates?in?months
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="start"></param>
          ????????
          /// ? <param?name="end"></param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ? int ?AgeInMonths(DateTime?start,?DateTime?end)
          ????????{
          ????????????
          return ?(end.Year - start.Year)? * ? 12 ? + ?(end.Month - start.Month);
          ????????}

          ????????
          /// ? <summary>
          ????????
          /// ?Return?the?duration?between?two?dates?in?years
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="start"></param>
          ????????
          /// ? <param?name="end"></param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ? int ?AgeInYears(DateTime?start,?DateTime?end)
          ????????{
          ????????????
          return ?AgeInMonths(start,?end)? / ? 12 ;
          ????????}

          ????????
          /// ? <summary>
          ????????
          /// ?Return?the?duration?until?today?in?months
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="start"></param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ? int ?AgeInMonths(DateTime?start)
          ????????{
          ????????????
          return ?AgeInMonths(start,?DateTime.Today);
          ????????}

          ????????
          /// ? <summary>
          ????????
          /// ?Return?the?duration?until?today?in?years
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="start"></param>
          ????????
          /// ? <param?name="end"></param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ? int ?AgeInYears(DateTime?start)
          ????????{
          ????????????
          return ?AgeInYears(start,?DateTime.Today);
          ????????}

          ????????
          /// ? <summary>
          ????????
          /// ?Calculate?the?easter?holiday?for?a?year
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="year"></param>
          ????????
          /// ? <returns></returns>
          ???????? public ? static ?DateTime?Easter( int ?year)
          ????????{
          ????????????
          int ?month;
          ????????????
          int ?day;

          ????????????
          int ?k1? = ? 24 ;
          ????????????
          int ?k2? = ? 5 ;

          ????????????
          int ?r1? = ?year? % ? 19 ;
          ????????????
          int ?r2? = ?year? % ? 4 ;
          ????????????
          int ?r3? = ?year? % ? 7 ;
          ????????????
          int ?r4? = ?( 19 ? * ?r1? + ?k1)? % ? 30 ;
          ????????????
          int ?r5? = ?( 2 ? * ?r2? + ? 4 ? * ?r3? + ? 6 ? * ?r4? + ?k2)? % ? 7 ;
          ????????????
          int ?A?? = ? 22 ? + ?r4? + ?r5;

          ????????????
          if ?(A? < ? 32 )?
          ????????????{
          ????????????????month?
          = ? 3 ;
          ????????????????day?
          = ?A;
          ????????????}
          ????????????
          else
          ????????????{
          ????????????????A?
          = ?r4? + ?r5? - ? 9 ;
          ????????????????month?
          = ? 4 ;
          ????????????????
          if ?(A? == ? 26 )
          ????????????????????day?
          = ? 19 ;
          ????????????????
          else ? if ?(A? != ? 25 )?
          ????????????????????day?
          = ?A;
          ????????????????
          else ? if ?(r4? == ? 18 ?? && ?r5? > ? 10 )
          ????????????????????day?
          = ? 18 ;
          ????????????????
          else
          ????????????????????day?
          = ? 25 ;
          ????????????}
          ????????????
          ????????????
          return ? new ?DateTime(year,month,day);
          ????????}
          ????}

          posted on 2007-01-22 13:42 record java and net 閱讀(210) 評論(0)  編輯  收藏 所屬分類: 常用配置代碼

          導航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 游戏| 湖州市| 永丰县| 屯留县| 洛川县| 北海市| 祁连县| 任丘市| 铁力市| 巴中市| 内江市| 长泰县| 济宁市| 黔江区| 神农架林区| 普兰县| 荥经县| 泽州县| 山东| 策勒县| 莱阳市| 肇源县| 东乡族自治县| 华坪县| 额尔古纳市| 南靖县| 巴南区| 定日县| 徐水县| 启东市| 昭苏县| 江城| 乌拉特后旗| 开平市| 大英县| 盐边县| 郓城县| 永丰县| 辽源市| 合作市| 乳源|