日期计算

简单日期计算类:
日期大小比较
日期之间天数计算
package org.jf.alg;


/**
 * 
 * @author junfeng.chen
 *
 */
public class Date {

	private int year;
	private int month;
	private int day;
	
	private static final int days_array[]= {31,28,31,30,31,30,31,31,30,31,30,31};
	private static final int days_leap_array[]= {31,29,31,30,31,30,31,31,30,31,30,31};
	private static final String  MONTH_NAME[] = {"January","Febuary", "March",  "April", "May", "June", "July",   "August",  "September", "October","November","December"};   
	private static final String WEEK_NAME[] = {"Monday","Tuesday", "Wednesday","Thursday", "Friday","Saturday","Sunday"}; 
    
	public Date(int year,int month,int day)
	{
		this.year = year;
		boolean leap = Date.isLeapYear(this.year);
		this.month = month;
		if(this.month%12==0)
			this.month = 12;
		else this.month = this.month%12;
		if(leap)
		{
			this.day = day%days_leap_array[this.month-1]==0?days_leap_array[this.month-1]:day%days_leap_array[this.month-1];//day<=days_leap_array[this.month-1]?day:days_leap_array[this.month-1];
		}else
		{
			this.day = day%days_array[this.month-1]==0?days_array[this.month-1]:day%days_array[this.month-1];
		}
	}
	
	public Date()
	{
		this.year = 1970;
		this.month = 1;
		this.day =1;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		
		this.month = month;

		boolean leap = Date.isLeapYear(this.year);
		if(this.month%12==0)
			this.month = 12;
		else this.month = this.month%12;
		
		//check day
		if(leap)
		{
			this.day = day%days_leap_array[this.month-1]==0?days_leap_array[this.month-1]:day%days_leap_array[this.month-1];//day<=days_leap_array[this.month-1]?day:days_leap_array[this.month-1];
		}else
		{
			this.day = day%days_array[this.month-1]==0?days_array[this.month-1]:day%days_array[this.month-1];
		}
	}

	public int getDay() {
		return day;
	}

	/**
	 * date filed set seq --> year month day
	 * you should granted that year filed and month filed have set
	 * @param day
	 */
	public void setDay(int day) {
		
		this.day = day;
		
		boolean isleap = isLeapYear(year);
		if(isleap)
		{
			this.day = day%days_leap_array[this.month-1]==0?days_leap_array[this.month-1]:day%days_leap_array[this.month-1];//day<=days_leap_array[this.month-1]?day:days_leap_array[this.month-1];
		}else
		{
			this.day = day%days_array[this.month-1]==0?days_array[this.month-1]:day%days_array[this.month-1];
		}
		
		
	}
	
	
	public Date addDay(int days)
	{
		if(days > 0)
		{
			//上补齐为本月末一天
			int thismonthday = this.monthDays(this.month,isLeapYear(this));
			int d = thismonthday - this.day;
			if(days>=d)
			{
				this.day = thismonthday;
				days = days - d;
			}
			else
			{
				this.day+=days;
				days = 0;
			}
			
			while(days > 0)
			{
				int nextmonth = this.month+1;
				int nextyear = this.year;
				if(nextmonth>12)
				{
					nextmonth = 1;
					nextyear += 1;
				}
				boolean ran = isLeapYear(nextyear);
				int nextmonthday = this.monthDays(nextmonth,ran);
				if(days>=nextmonthday)
				{
					this.month = nextmonth;
					this.year = nextyear;
					days -= nextmonthday;
					this.day = nextmonthday;

				}else
				{
					this.month = nextmonth;
					this.day = days;
					days = 0;

				}
			}
		}else
		{			
			days*=-1;
			//下补齐为上月最后一天(如果可能)
			if(this.day<=days)
			{
				days -= this.day;
				int pre_year = this.year;
				int pre_month =this.month-1;
				if(pre_month == 0)
				{
					pre_month = 12;
					pre_year -- ;
				}
				int pre_month_day = monthDays(pre_month,isLeapYear(pre_year));
				this.year = pre_year;
				this.month = pre_month;
				this.day = pre_month_day;
			}else
			{
				
				this.day -= days;
				days = 0;
			}
			
			
			//按月递减
			while(days>0)
			{
				int pre_month = this.month - 1;
				int pre_year = year;
				if(pre_month==0)
				{
					pre_month = 12;
			    	pre_year = year -1;
				}
				int pre_month_day = this.monthDays(pre_month, isLeapYear(pre_year));
				
				if(days>=this.day)
				{
					days -= this.day;
					this.year = pre_year;
					this.month = pre_month;
					this.day = pre_month_day;
					
				}else
				{
					this.day -= days;
					days = 0;
				}
			}
			
		}
		
		return this;
	}
	
	
	/**
	 * 
	 * @param months
	 * @return
	 */
	public Date addMonth(int months)
	{
		int addYear = months/12;
		int addMonth = months%12;
		
		
		if(months>0)
		{
			this.year += addYear;
			this.month += addMonth;
			
			if(this.month>12)
			{
				this.year ++;
				this.month = this.month - 12;
			}
		
		}else
		{
			this.year +=addYear;
			if(this.month+addMonth<=0)
			{
				this.year --;
				this.month = 12 + this.month+addMonth;
			}else
			{
				this.month += addMonth;
			}
		}
		
		
		boolean isleap = isLeapYear(year);
		if(isleap)
		{
			if(day>Date.days_leap_array[month-1])
				day = Date.days_leap_array[month-1];
		}
		else 
		{
			if(day>Date.days_array[month-1])
				day = Date.days_array[month-1];
		}
		
		return this;
	}
	
	
	public Date addYear(int year)
	{
		this.year +=year;
		boolean leap = Date.isLeapYear(this.year);
		if(leap)
		{
			if(this.day > Date.days_leap_array[this.month-1])
				this.day = Date.days_leap_array[this.month-1];
		}else
		{
			if(this.day > Date.days_array[this.month-1])
				this.day = Date.days_array[this.month-1];
		}
		
		return this;
	}
	
	
	@Deprecated
	public static int daysBetween(Date date1,Date date2)
	{
		return date1.days() - date2.days();
	}
	
	public String weekDay()
	{
		int week_num = (day + 2*month + 3*(month+1)/5 + year + year/4 - year/100 + year/400) % 7;   

		return Date.WEEK_NAME[week_num];
	}
	
	public int dateDiff(Date date)
	{
		int nd , nm , ny; //new_day, new_month, new_year   
	    int od , om , oy ; //old_day, oldmonth, old_year   
		
		 nm = (date.month + 9) % 12;   
		 ny = date.year - nm/10;   
	     nd = 365*ny + ny/4 - ny/100 + ny/400 + (nm*306 + 5)/10 + (date.day - 1);   
    
		 om = (month + 9) % 12;   
		 oy = year - om/10;   
		 od = 365*oy + oy/4 - oy/100 + oy/400 + (om*306 + 5)/10 + (day - 1);   

		 return od - nd;
	}
	
	public int cmp(Date date)
	{
		if (this.year > date.year)
			return 1;
		else if (this.year == date.year) {
			if (this.month > date.month)
				return 1;
			else if (this.month == date.month) {
				if (this.day > date.day)
					return 1;
				else if (this.day == date.day)
					return 0;
//				return -1;
			}
//			return -1;
		}

		return -1;
		
			
	}
	
	public static boolean isLeapYear(int year)
	{
		return (year%4==0&&year%100!=0) || year%400 == 0;
	}
	
	public static boolean isLeapYear(Date date)
	{
		return (date.year%4==0&&date.year%100!=0) || date.year % 400 == 0 ;
	}
	
	private int monthDays(int month,boolean run)
	{
		if(run)
			return days_leap_array[month-1];
		return days_array[month-1];
	}
	
	public String toString()
	{
		return this.year+"-"+this.month+"-"+this.day;
	}
	
	int days()
	{
		int year = this.year;
		int month = this.month;
		int day = this.day;
		int days = 0;
		days += day;
		month --;
		if(month==0)
		{
			month = 12;
			year --;
		}
		int monthday = this.monthDays(month, isLeapYear(year));
		
		while(year>0)
		{
			days+=monthday;
			month --;
			if(month==0)
			{
				month = 12;
				year --;
			}
			monthday = this.monthDays(month, isLeapYear(year));
		}
		return days;
	}
	
	
}

你可能感兴趣的:(日期)