Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class Date here.
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8. public class Date
  9. {
  10.     private int _day;
  11.     private int _month;
  12.     private int _year;
  13.     private final int JAN=31;
  14.     private final int FEB=28;
  15.     private final int MAR=31;
  16.     private final int APR=30;
  17.     private final int MAY=31;
  18.     private final int JUN=30;
  19.     private final int JUL=31;
  20.     private final int AUG=31;
  21.     private final int SEP=30;
  22.     private final int OCT=31;
  23.     private final int NOV=30;
  24.     private final int DEC=31;
  25.  
  26.     //constructors:
  27.     /**
  28.      * creates a new Date object
  29.      * @param _day the day in the month(1-31)
  30.      * @param _month the month in the year
  31.      * @param _year the year (in 4 digits)
  32.      */
  33.     public Date(int day, int month, int year) {
  34.         _day = day;
  35.         _month = month;
  36.         _year = year;
  37.  
  38.         if(checkDate(this._day,this._month,this._year)==false)
  39.         {
  40.             _day=1;
  41.             _month=1;
  42.             _year=2000;
  43.         }
  44.  
  45.     } //
  46.     /**
  47.      * Copy Constructor
  48.      * @param passport to be copied
  49.      */
  50.     public Date(Date other){
  51.         this._day = other._day;
  52.         this._month = other._month;
  53.         this._year = other._year;
  54.     }
  55.  
  56.     /** gets the year */
  57.     public int getYear(){
  58.         return _year;
  59.     }
  60.  
  61.     /** gets the month */
  62.     public int getMonth(){
  63.         return _month;
  64.     }
  65.  
  66.     /** gets the Day */
  67.     public int getDay(){
  68.         return _day;
  69.     }
  70.  
  71.     /** sets the year
  72.      * @param yearToSet the value to be set
  73.      */
  74.     public void setYear(int yearToSet){
  75.         if(checkDate(_day, _month, yearToSet)==true)
  76.             _year = yearToSet;
  77.     }
  78.  
  79.     /** set the month
  80.      * @param monthToSet the value to be set
  81.      */
  82.     public void setMonth(int monthToSet){
  83.         if(checkDate(_day, monthToSet, _year)==true)
  84.             _month = monthToSet;
  85.     }
  86.  
  87.     /** sets the day
  88.      *  @param dayToSet the value to be set
  89.      */
  90.     public void setDay(int dayToSet){
  91.         if(checkDate(dayToSet, _month, _year)==true)
  92.             _day = dayToSet;
  93.     }
  94.  
  95.     private boolean checkDate(int day, int month, int year) { //Check Date
  96.         _day = day;
  97.         _month = month;
  98.         _year = year;
  99.         int _check=0;
  100.         if((_day>31||_day<1)||(_month>12||_month<1)||(_year>9999||_year<1000))
  101.             _check=1;
  102.         else
  103.         {
  104.             if(_month==2) //Check for FEBRUARY
  105.             {
  106.                 if(_day>29 || _day<28)
  107.                     _check=1;
  108.                 else
  109.                 {
  110.                     if(_year%4==0)
  111.                     {
  112.                         if(_year%100>0 && _year%400>0)
  113.                             _check=1;
  114.                     }
  115.                 }
  116.             }
  117.  
  118.             if((_month==4 || _month==6 || _month==9 || _month==11) && _day>30) //Check for 30days monthes
  119.                 _check=1;
  120.         }
  121.         if(_check==1)
  122.             return false;
  123.         else    
  124.             return true;
  125.     }
  126.  
  127.     public boolean equals(Date other){ //Check if same
  128.         if((this._day == other._day) && (this._month == other._month) && (this._year == other._year))
  129.             return true;
  130.         return false;
  131.     }
  132.  
  133.     /**
  134.      * checks if this date comes before a given date
  135.      * @param other the given date
  136.      * @retun true iff this date comes before other
  137.      */
  138.     public boolean before(Date other) {
  139.         if (_year < other._year)
  140.             return true;
  141.         if (_year == other._year) {
  142.             if (_month < other._month)
  143.                 return true;
  144.             if (_month == other._month && _day < other._day)
  145.                 return true;
  146.         }
  147.         return false;
  148.     }
  149.  
  150.     /**
  151.      * checks if this date comes after a given date
  152.      * @param other the given date
  153.      * @retun true iff this date comes before other
  154.      */
  155.     public boolean after(Date other) {
  156.         if(before(other)==false)
  157.             return true;
  158.         return false;
  159.     }
  160.  
  161.     /**
  162.      * checks the difference in days between dates
  163.      * @param other the given date
  164.      * @retun the difference in days between dates
  165.      */
  166.     public int difference(Date other) {
  167.         int _sumDt=calculateDate(_day,_month,_year); //this date
  168.         int _sumDo=calculateDate(other._day,other._month,other._year); //other date
  169.         if (_sumDt>_sumDo)
  170.             return _sumDt-_sumDo;
  171.         else
  172.             return _sumDo-_sumDt;
  173.     }
  174.  
  175.     /**
  176.      * Given tomorrow date
  177.      * @retun tomorrow date
  178.      */
  179.  
  180.     public Date tomorrow()
  181.     {
  182.         if(checkDate(_day+1,_month,_year)==true)
  183.         {
  184.                 Date dateN = new Date(_day+1,_month,_year);
  185.             return dateN;
  186.         }
  187.         else
  188.         if(checkDate(1,_month+1,_year)==true)
  189.         {
  190.                 Date dateN = new Date(1,_month+1,_year);
  191.             return dateN;
  192.         }
  193.         else
  194.         if(checkDate(1,1,_year+1)==true)
  195.         {
  196.                 Date dateN = new Date(1,1,_year+1);
  197.             return dateN;
  198.         }
  199.         else
  200.         {
  201.                 Date dateN = new Date(_day+1,_month,_year);
  202.             return dateN;
  203.         }
  204.     }
  205.  
  206.     /**
  207.      * @return String that represents this date
  208.      * in the following format:
  209.      * day.month.year (30/9/1917)
  210.      */
  211.     public String toString() {
  212.         String s="";
  213.         if (_day<10)
  214.             s+="0"+_day+"/";
  215.         else
  216.             s+=_day+"/";
  217.         if (_month<10)
  218.             s+="0"+_month+"/";
  219.         else
  220.             s+=_month+"/";    
  221.         s+=_year;  
  222.  
  223.         return s;
  224.     }  
  225.     // computes the day number since the beginning of the Christian counting of years
  226.     private int calculateDate ( int day, int month, int year) {
  227.         if (month < 3) {
  228.             year--;
  229.             month = month + 12;
  230.         }
  231.         return 365 * year + year/4 - year/100 + year/400 + ((month+1) * 306)/10 + (day - 62);
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement