Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package programming.set6.date;
- /**
- * A class that provides basic functions to handle dates.
- *
- * @author Bjarne
- */
- public class Date {
- /**
- * the year of an instance of the class
- */
- private int year;
- /**
- * the month of an instance of the class
- */
- private int month;
- /**
- * the day of an instance of the class
- */
- private int day;
- /**
- * An array that contains the number of days that each month has, where the
- * 0-th element represents January and the 11-th element represents
- * December.
- */
- private static final int[] DaysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31,
- 30, 31, 30, 31 };
- /**
- * A second array that contains the number of days for each month within a
- * leap year.
- */
- private static final int[] DaysInMonth_LeapYear = { 31, 29, 31, 30, 31, 30,
- 31, 31, 30, 31, 30, 31 };
- /**
- * An array that contains the names of the months.
- */
- private static final String[] MonthNames = { "January", "February",
- "March", "April", "May", "June", "July", "August", "September",
- "October", "November", "December" };
- /**
- * Returns the number of days in a given month.
- *
- * @param year
- * the year is needed to check whether it is a leap year
- * @param month
- * the given month
- * @return days in the given month
- */
- public static int getDaysInMonth(int year, int month) {
- // return 0 for an invalid month
- if (month < 1 || month > 12) {
- return 0;
- }
- // check for leap year
- if (year % 4 == 0 && year % 100 != 0 || year % 4 == 0
- && year % 100 == 0 && year % 400 == 0) {
- // If the year is a leap year, return the value from the leap year array.
- // As January is the first month, but the 0-th element in the array,
- // we need to subtract 1 from month to get the correct value.
- return DaysInMonth_LeapYear[month - 1];
- }
- // If the year isn't a leap year, use the default array.
- else {
- return DaysInMonth[month - 1];
- }
- }
- /**
- * Returns true for a valid date and false for an invalid one.
- *
- * @param year
- * the year of the given date
- * @param month
- * the month of the given date
- * @param day
- * the day of the given date
- * @return boolean
- */
- public static boolean validate(int year, int month, int day) {
- // check for valid month in the range from 1 - 12
- // and for a day greater 0.
- if (month > 0 && month < 13 && day > 0) {
- // If we have a leap year, compare the value of the given
- // day to the corresponding value in the leap year array.
- if (year % 4 == 0 && year % 100 != 0 || year % 4 == 0
- && year % 100 == 0 && year % 400 == 0) {
- if (day <= DaysInMonth_LeapYear[month - 1]) {
- return true;
- }
- // otherwise, use the default array
- } else if (day <= DaysInMonth[month - 1]) {
- return true;
- }
- }
- // if the month is outside of the range 1 - 12, or the day is smaller than one,
- // or the value of the given day is greater than the number of days in the respective month,
- // return false.
- return false;
- }
- /**
- * To be able to declare further constructors, we need to declare this 0 -
- * constructor. (Would have been nice to know before, though)
- */
- public Date() {
- year = 0;
- month = 0;
- day = 0;
- }
- /**
- * A constructor that initializes Date with the parameters year, month and
- * day. If the given values don't represent a valid date, an
- * IllegalArgumentException is thrown.
- *
- * @param year
- * the year of the date
- * @param month
- * the month of the date
- * @param day
- * the day of the date
- */
- public Date(int year, int month, int day) {
- if (validate(year, month, day) == false) {
- throw new IllegalArgumentException("This is not a valid date.");
- }
- this.year = year;
- this.month = month;
- this.day = day;
- }
- /**
- * Getter method to obtain the year of a date.
- *
- * @return year
- */
- public int getYear() {
- return this.year;
- }
- /**
- * Getter method to obtain the month of a date.
- *
- * @return month
- */
- public int getMonth() {
- return this.month;
- }
- /**
- * Getter method to obtain the day of a date.
- *
- * @return day
- */
- public int getDay() {
- return this.day;
- }
- /**
- * Calculates the position of a given day within the given year
- *
- * @return position of the day
- */
- public int dayOfYear() {
- // The variable n is used to sum up all days of the previous months.
- int n = 0;
- // as usual, if the year is a leap year, the leap year array is used.
- if (year % 4 == 0 && year % 100 != 0 || year % 4 == 0
- && year % 100 == 0 && year % 400 == 0) {
- for (int i = 0; i < this.month - 1; i++) {
- n += DaysInMonth_LeapYear[i];
- }
- } else {
- for (int i = 0; i < this.month - 1; i++) {
- n += DaysInMonth[i];
- }
- }
- // We add the days of our given date to n and thus get the position of the day within the year.
- int DayOfYear = this.day + n;
- return DayOfYear;
- }
- /**
- * Calculates the difference (in days) between two dates within the same
- * year
- *
- * @param other
- * the other date
- * @return difference of the positions
- */
- public int sameYearDiff(Date other) {
- // The method will only work for dates with the same year
- // and otherwise return 0.
- if (this.year - other.year != 0) {
- return 0;
- }
- int diff = other.dayOfYear() - this.dayOfYear();
- return diff;
- }
- /**
- * Creates a string that displays a given date. The array that contains the names
- * of the months is used here.
- */
- public String toString() {
- String month = MonthNames[this.month - 1];
- String date = month + " " + this.day + ", " + this.year;
- return date;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement