phantom900

Date.java

Mar 23rd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.59 KB | None | 0 0
  1. /**
  2.  * This class represents a Date object.
  3.  *
  4.  * Author: Daniel Litvak
  5.  * Date: 1.4.2018
  6.  */
  7. public class Date
  8. {
  9.     private int _day;
  10.     private int _month;
  11.     private int _year;
  12.  
  13.     private final int DEFAULT_DAY = 1;
  14.     private final int DEFAULT_MONTH = 1;
  15.     private final int DEFAULT_YEAR = 2000;
  16.     private final int MAX_DAY = 31;
  17.     private final int MAX_MONTH = 12;
  18.     private final int MAX_YEAR = 9999;
  19.     private final int MIN_DAY = 1;
  20.     private final int MIN_MONTH = 1;
  21.     private final int MIN_YEAR = 1000;
  22.     // Constructors
  23.  
  24.     /**
  25.      * Creates a new Date object if the date is valid, otherwise creates the date 1/1/2000.
  26.      *
  27.      * @param day   the day in the month (1-31)
  28.      * @param month the month in the year (1-12)
  29.      * @param year  the year (4 digits)
  30.      */
  31.     public Date(int day, int month, int year)
  32.     {
  33.         _day = day;
  34.         _month = month;
  35.         _year = year;
  36.  
  37.         // If the date is invalid set the date to 1/1/2000
  38.         if (!isValid(this))
  39.         {
  40.             _day = DEFAULT_DAY;
  41.             _month = DEFAULT_MONTH;
  42.             _year = DEFAULT_YEAR;
  43.         }
  44.     }
  45.  
  46.     /**
  47.      * Copy constructor.
  48.      *
  49.      * @param other the date to be copied
  50.      */
  51.     public Date(Date other)
  52.     {
  53.         _day = other._day;
  54.         _month = other._month;
  55.         _year = other._year;
  56.     }
  57.  
  58.     /**
  59.      * Check if 2 dates are the same.
  60.      *
  61.      * @param other the date to compare this date to
  62.      * @return true if the dates are the same
  63.      */
  64.     public boolean equals(Date other)
  65.     {
  66.         // Return true if all of the attributes are the same
  67.         return (_year == other._year) && (_month == other._month) &&
  68.                 (_day == other._day);
  69.     }
  70.  
  71.     /**
  72.      * Check if this date is before other date.
  73.      *
  74.      * @param other the date to compare to
  75.      * @return true if this date is before other date
  76.      */
  77.     public boolean before(Date other)
  78.     {
  79.         if (_year < other._year)
  80.         {
  81.             return true;
  82.         }
  83.         // Continue checking if the years are the same
  84.         else if (_year == other._year)
  85.         {
  86.             if (_month < other._month)
  87.             {
  88.                 return true;
  89.             }
  90.             // Continue checking if the months are the same
  91.             else if (_month == other._month)
  92.             {
  93.                 if (_day < other._day)
  94.                 {
  95.                     return true;
  96.                 }
  97.             }
  98.         }
  99.         return false;
  100.     }
  101.  
  102.     /**
  103.      * Check if this date is after other date.
  104.      *
  105.      * @param other the date to compare to
  106.      * @return true if this date is after other date
  107.      */
  108.     public boolean after(Date other)
  109.     {
  110.         // If the other is before this date it means that this date
  111.         // is after the compare date
  112.         return other.before(this);
  113.     }
  114.  
  115.     /**
  116.      * Calculates the difference in days between two dates.
  117.      *
  118.      * @param other the date to calculate the difference between
  119.      * @return the number of days between the dates
  120.      */
  121.     public int difference(Date other)
  122.     {
  123.         return Math.abs(calculateDate(_day, _month, _year) -
  124.                 calculateDate(other._day, other._month, other._year));
  125.     }
  126.  
  127.     /**
  128.      * Returns a String that represents this date.
  129.      *
  130.      * @return String that represents this date in the following format:
  131.      * day/month/year for example: 2/3/1998
  132.      */
  133.     public String toString()
  134.     {
  135.         return _day + "/" + _month + "/" + _year;
  136.     }
  137.  
  138.     /**
  139.      * Calculate the day of the week that this date occurs on 0-Saturday 1-Sunday 2-Monday etc.
  140.      *
  141.      * @return the day of the week that this date occurs on
  142.      */
  143.     public int dayInWeek()
  144.     {
  145.         int d = _day;
  146.         int m = _month;
  147.         int year = _year;
  148.  
  149.         // The formula deals with January and February as 13 and 14 of the previous year
  150.         if (m < 3)
  151.             m = m + 2;
  152.         year--;
  153.         // Last 2 digits of year
  154.         int y = year % 100;
  155.         int c = firstDigitsOfYear(year);
  156.  
  157.         // Using a given formula to calculate the day in week
  158.         return Math.floorMod((d + (26 * (m + 1)) / 10 + y + y / 4 + c / 4 - 2 * c), 7);
  159.     }
  160.  
  161.     // Getters
  162.  
  163.     /**
  164.      * Gets the day.
  165.      *
  166.      * @return the day
  167.      */
  168.     public int getDay()
  169.     {
  170.         return _day;
  171.     }
  172.  
  173.     /**
  174.      * Gets the month.
  175.      *
  176.      * @return the month
  177.      */
  178.     public int getMonth()
  179.     {
  180.         return _month;
  181.     }
  182.  
  183.     /**
  184.      * Gets the year.
  185.      *
  186.      * @return the year
  187.      */
  188.     public int getYear()
  189.     {
  190.         return _year;
  191.     }
  192.  
  193.     // Setters
  194.  
  195.     /**
  196.      * Sets the day (only if date remains valid).
  197.      *
  198.      * @param dayToSet the day value to be set
  199.      */
  200.     public void setDay(int dayToSet)
  201.     {
  202.         // Keep previous day in case the new date is invalid
  203.         int previousDay = _day;
  204.  
  205.         _day = dayToSet;
  206.  
  207.         // If the new date is invalid undo the change
  208.         if (!isValid(this))
  209.         {
  210.             _day = previousDay;
  211.         }
  212.     }
  213.  
  214.     /**
  215.      * Set the month (only if date remains valid).
  216.      *
  217.      * @param monthToSet the month value to be set
  218.      */
  219.     public void setMonth(int monthToSet)
  220.     {
  221.         // Keep previous month in case the new date is invalid
  222.         int previuosMonth = _month;
  223.  
  224.         _month = monthToSet;
  225.  
  226.         // If the new date is invalid undo the change
  227.         if (!isValid(this))
  228.         {
  229.             _month = previuosMonth;
  230.         }
  231.     }
  232.  
  233.     /**
  234.      * Sets the year (only if date remains valid).
  235.      *
  236.      * @param yearToSet the year value to be set
  237.      */
  238.     public void setYear(int yearToSet)
  239.     {
  240.         // Keep previous year in case the new date is invalid
  241.         int previuosYear = _year;
  242.  
  243.         _year = yearToSet;
  244.  
  245.         // If the new date is invalid undo the change
  246.         if (!isValid(this))
  247.         {
  248.             _year = previuosYear;
  249.         }
  250.     }
  251.  
  252.     /**
  253.      * Returns the first 2 digits of the year.
  254.      *
  255.      * @param year the year to get the digits from
  256.      * @return the first 2 digits of this year
  257.      */
  258.     private int firstDigitsOfYear(int year)
  259.     {
  260.         // If the year is more than 2 digits drop the last digit until
  261.         // there are no more than 2 digits
  262.         while (year >= 100)
  263.         {
  264.             year = year / 10;
  265.         }
  266.         // Return the year after it is only the first 2 digits
  267.         return year;
  268.     }
  269.  
  270.     /**
  271.      * Checks the date to make sure it is valid.
  272.      *
  273.      * @param date The date to check
  274.      * @return true if the date is valid
  275.      */
  276.     private boolean isValid(Date date)
  277.     {
  278.         // Year must be positive and only 4 digits
  279.         if (date._year < MIN_YEAR || date._year > MAX_YEAR)
  280.         {
  281.             return false;
  282.         }
  283.         // Month can only be between 1 to 12
  284.         if (date._month < MIN_MONTH || date._month > MAX_MONTH)
  285.         {
  286.             return false;
  287.         }
  288.         // Day can be only between 1 to 31
  289.         if (date._day < MIN_DAY || date._day > MAX_DAY)
  290.         {
  291.             return false;
  292.         }
  293.  
  294.         // check if the day fits the month and year
  295.         return isValidDay(date);
  296.     }
  297.  
  298.     /**
  299.      * Checks that the day in the date is valid. Assumes that day, month, and year
  300.      * are within the limitations:
  301.      * day: 1 - 31
  302.      * month: 1 - 12
  303.      * year: 1000 - 9999
  304.      *
  305.      * @param date the date to check
  306.      * @return true if the day is valid, false otherwise
  307.      */
  308.     private boolean isValidDay(Date date)
  309.     {
  310.         int daysInMonth = 0;
  311.         switch (date._month)
  312.         {
  313.             case 2:
  314.                 // February might have 28 or 29 days
  315.                 if (isLeapYear(date._year))
  316.                 {
  317.                     daysInMonth = 29;
  318.                 }
  319.                 else
  320.                 {
  321.                     daysInMonth = 28;
  322.                 }
  323.                 break;
  324.             case 4:
  325.                 // April has 30 days
  326.                 daysInMonth = 30;
  327.                 break;
  328.             case 6:
  329.                 // June has 30 days
  330.                 daysInMonth = 30;
  331.                 break;
  332.             case 9:
  333.                 // September has 30 days
  334.                 daysInMonth = 30;
  335.                 break;
  336.             case 11:
  337.                 // November has 30 days
  338.                 daysInMonth = 30;
  339.                 break;
  340.             default:
  341.                 // 31 days in all the other months
  342.                 daysInMonth = 31;
  343.                 break;
  344.         }
  345.  
  346.         // Return true if the day is within the number of days in the month
  347.         return date._day <= daysInMonth;
  348.     }
  349.  
  350.     /**
  351.      * Check if the year is a leap year.
  352.      *
  353.      * @param year the year to check
  354.      * @return true if the year is a leap year
  355.      */
  356.     private boolean isLeapYear(int year)
  357.     {
  358.         return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  359.     }
  360.  
  361.     /**
  362.      * Computes the day number since the beginning of the Christian counting of years.
  363.      *
  364.      * @param day   the day
  365.      * @param month the month
  366.      * @param year  the year
  367.      * @return number of days since 0/0/0
  368.      */
  369.     private int calculateDate(int day, int month, int year)
  370.     {
  371.         if (month < 3)
  372.         {
  373.             year--;
  374.             month = month + 12;
  375.         }
  376.         return 365 * year + year / 4 - year / 100 + year / 400 +
  377.                 ((month + 1) * 306 / 10 + (day - 62));
  378.     }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment