Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class Date here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class Date
- {
- private int _day;
- private int _month;
- private int _year;
- private final int JAN=31;
- private final int FEB=28;
- private final int MAR=31;
- private final int APR=30;
- private final int MAY=31;
- private final int JUN=30;
- private final int JUL=31;
- private final int AUG=31;
- private final int SEP=30;
- private final int OCT=31;
- private final int NOV=30;
- private final int DEC=31;
- //constructors:
- /**
- * creates a new Date object
- * @param _day the day in the month(1-31)
- * @param _month the month in the year
- * @param _year the year (in 4 digits)
- */
- public Date(int day, int month, int year) {
- _day = day;
- _month = month;
- _year = year;
- if(checkDate(this._day,this._month,this._year)==false)
- {
- _day=1;
- _month=1;
- _year=2000;
- }
- } //
- /**
- * Copy Constructor
- * @param passport to be copied
- */
- public Date(Date other){
- this._day = other._day;
- this._month = other._month;
- this._year = other._year;
- }
- /** gets the year */
- public int getYear(){
- return _year;
- }
- /** gets the month */
- public int getMonth(){
- return _month;
- }
- /** gets the Day */
- public int getDay(){
- return _day;
- }
- /** sets the year
- * @param yearToSet the value to be set
- */
- public void setYear(int yearToSet){
- if(checkDate(_day, _month, yearToSet)==true)
- _year = yearToSet;
- }
- /** set the month
- * @param monthToSet the value to be set
- */
- public void setMonth(int monthToSet){
- if(checkDate(_day, monthToSet, _year)==true)
- _month = monthToSet;
- }
- /** sets the day
- * @param dayToSet the value to be set
- */
- public void setDay(int dayToSet){
- if(checkDate(dayToSet, _month, _year)==true)
- _day = dayToSet;
- }
- private boolean checkDate(int day, int month, int year) { //Check Date
- _day = day;
- _month = month;
- _year = year;
- int _check=0;
- if((_day>31||_day<1)||(_month>12||_month<1)||(_year>9999||_year<1000))
- _check=1;
- else
- {
- if(_month==2) //Check for FEBRUARY
- {
- if(_day>29 || _day<28)
- _check=1;
- else
- {
- if(_year%4==0)
- {
- if(_year%100>0 && _year%400>0)
- _check=1;
- }
- }
- }
- if((_month==4 || _month==6 || _month==9 || _month==11) && _day>30) //Check for 30days monthes
- _check=1;
- }
- if(_check==1)
- return false;
- else
- return true;
- }
- public boolean equals(Date other){ //Check if same
- if((this._day == other._day) && (this._month == other._month) && (this._year == other._year))
- return true;
- return false;
- }
- /**
- * checks if this date comes before a given date
- * @param other the given date
- * @retun true iff this date comes before other
- */
- public boolean before(Date other) {
- if (_year < other._year)
- return true;
- if (_year == other._year) {
- if (_month < other._month)
- return true;
- if (_month == other._month && _day < other._day)
- return true;
- }
- return false;
- }
- /**
- * checks if this date comes after a given date
- * @param other the given date
- * @retun true iff this date comes before other
- */
- public boolean after(Date other) {
- if(before(other)==false)
- return true;
- return false;
- }
- /**
- * checks the difference in days between dates
- * @param other the given date
- * @retun the difference in days between dates
- */
- public int difference(Date other) {
- int _sumDt=calculateDate(_day,_month,_year); //this date
- int _sumDo=calculateDate(other._day,other._month,other._year); //other date
- if (_sumDt>_sumDo)
- return _sumDt-_sumDo;
- else
- return _sumDo-_sumDt;
- }
- /**
- * Given tomorrow date
- * @retun tomorrow date
- */
- public Date tomorrow()
- {
- if(checkDate(_day+1,_month,_year)==true)
- {
- Date dateN = new Date(_day+1,_month,_year);
- return dateN;
- }
- else
- if(checkDate(1,_month+1,_year)==true)
- {
- Date dateN = new Date(1,_month+1,_year);
- return dateN;
- }
- else
- if(checkDate(1,1,_year+1)==true)
- {
- Date dateN = new Date(1,1,_year+1);
- return dateN;
- }
- else
- {
- Date dateN = new Date(_day+1,_month,_year);
- return dateN;
- }
- }
- /**
- * @return String that represents this date
- * in the following format:
- * day.month.year (30/9/1917)
- */
- public String toString() {
- String s="";
- if (_day<10)
- s+="0"+_day+"/";
- else
- s+=_day+"/";
- if (_month<10)
- s+="0"+_month+"/";
- else
- s+=_month+"/";
- s+=_year;
- return s;
- }
- // computes the day number since the beginning of the Christian counting of years
- private int calculateDate ( int day, int month, int year) {
- if (month < 3) {
- year--;
- month = month + 12;
- }
- return 365 * year + year/4 - year/100 + year/400 + ((month+1) * 306)/10 + (day - 62);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement