A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨占伟 中级黑马   /  2012-12-2 11:41  /  1468 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyTryParse
{
    class DateConstructors
    {
        static void Main(string[] args)
        {
            try
            {
                Date mydate = new Date(2001, 8, 29);
                Console.WriteLine("Day of year = " + mydate.DayOfYear);
                Console.ReadKey();
            }
            catch(Exception exc)
            {
                Console.WriteLine(exc);
            }
        }   
    }
    class Date
    {
                                                                                             //
        int year;
        int month;
        int day;
        static int[] MonthDays = new int[] { 0,31,59,90,120,151,181,212,243,273,304,334};
                                                                                             //
        public Date()
        {
            Year = 1600;
            Month = 1;
            Day = 1;
           
        }
        public Date(int year, int month, int day)
        {
            if((month == 2 && IsLeapYear(year) && day >29) || (month == 2 && !IsLeapYear(year) && day > 28) || ((month == 4 || month ==6 || month == 9 || month == 11) && day > 30))
            {
                throw new ArgumentOutOfRangeException("Day");
            }
            else
            {
                Year = year;
                Month = month;
                Day = day;
            }
        }
                                                                                             //
        public int Year
        {
            set
            {
                if (value < 1600)
                    throw new ArgumentOutOfRangeException("Year");
                else
                    year = value;
            }
            get
            {
                return year;
            }
        }
        public int Month
        {
            set
            {
                if (value < 1 || value > 12)
                    throw new ArgumentOutOfRangeException("Month");
                else
                    month = value;
            }
            get
            {
                return month;
            }
        }
        public int Day
        {
            set
            {
                if (value < 1 || value > 31)
                    throw new ArgumentOutOfRangeException("Day");
                else
                    day = value;
            }
            get
            {
                return day;
            }
        }
        public int DayOfYear
        {
            get
            {
                return MonthDays[month - 1] + day + (month > 2 && IsLeapYear(year) ? 1 : 0);
            }
        }
                                                                                            //
        public static bool IsLeapYear(int year)
        {
            return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
        }
    }
  
}

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

7 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
值得学习
回复 使用道具 举报
值得学习
回复 使用道具 举报
需要这么复杂么
回复 使用道具 举报
简便方法:
需导入 java.util.*; java.text.*;
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat(yyyy年MM月dd日 E  hh:mm:ss);
//这里会输出当前日期,而SimpleDateFormat 是我们用来控制输出格式的y代表年,M代表月份,d代表日 E代表星期几  h/m/s分别为 时分秒
String time=sdf.format(d);
System.out.pintln("time"+time);//会输出  2012年12月04日 星期一 17:06:30  需要控制星期输出,让其加一

Calendar ca=Calendar.getInstance();
System.out.println(ca);//会获取一个日历 包含各种日期信息  

System.out.pintln(c.get(Calendar.YEAR)+"年");//输出年份
System.out.pintln(c.get(Calendar.DAY_OF_YEAR)+"年");//输出是一年中的第几天
回复 使用道具 举报
看得累啊
回复 使用道具 举报
汪磊 中级黑马 2012-12-10 12:04:30
8#
没必要那么麻烦啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马