Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package programming.set6.date;
- /**
- * This class named date is a program that should return a date in a valid and
- * given format.
- *
- * @author Marielouise
- *
- */
- public class Date {
- private int year;
- private int month;
- private int day;
- private static final String[] MONTHS = { "January", "February", "March",
- "April", "May", "June", "July", "August", "September", "October",
- "November", "December" };
- /**
- * The method getDaysInMonth have two parameters.
- *
- * @param year
- * in which we are counting.
- * @param month
- * that we are counting the days.
- * @return the number of days of a given year.
- */
- public static int getDaysInMonth(int year, int month) {
- switch (month) {
- case 1: // "January";
- case 3: // "March";
- case 5: // "May";
- case 7: // "July";
- case 8: // "August";
- case 10: // "October";
- case 12: // "December";
- return 31;
- case 4: // "April";
- case 6: // "June";
- case 9: // "September";
- case 11: // "November";
- return 30;
- case 2: // "February";
- if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
- return 29;
- } else {
- return 28;
- }
- default: // out of range
- return 0;
- }
- }
- /**
- * This is the method validate with three parameters of type integer.
- *
- * @param year
- * of the date to validate.
- * @param month
- * of the date to validate.
- * @param day
- * of the date to validate.
- * @return a valid date.
- */
- public static boolean validate(int year, int month, int day) {
- // boolean validate == true;
- if (month < 1 || month > 12) {
- return false;
- } else if (day < 1 || day > getDaysInMonth(year, month)) {
- return false;
- } else {
- return true;
- }
- }
- /**
- * This method return a date.
- *
- * @param year
- * of the date.
- * @param month
- * of the date
- * @param day
- * of the date.
- */
- public Date(int year, int month, int day) {
- if (validate(year, month, day)) {
- this.year = year;
- this.month = month;
- this.day = day;
- } else {
- throw new IllegalArgumentException("This is not a valid date.");
- }
- }
- /**
- * This method get the day.
- *
- * @return a given day.
- */
- public int getDay() {
- return day;
- }
- /**
- * This method get the month.
- *
- * @return a given month.
- */
- public int getMonth() {
- return month;
- }
- /**
- * This method get the year.
- *
- * @return a given year.
- */
- public int getYear() {
- return year;
- }
- /**
- * This method calculate the numbers of days for a given date of the year.
- *
- * @return the number of days till this date or between two days.
- */
- public int dayOfYear() {
- int dayOfYear = 0;
- for (int i = 0; i < month; i++) {
- dayOfYear = dayOfYear + getDaysInMonth(year, i);
- }
- dayOfYear = dayOfYear + day;
- return dayOfYear;
- }
- /**
- * This method calculate how many days the other date is off.
- *
- * @param other
- * is the other date we need to make the difference.
- * @return the result in integer form.
- */
- public int sameYearDiff(Date other) {
- if (year == other.getYear()) {
- return other.dayOfYear() - dayOfYear();
- } else {
- return 0;
- }
- }
- /**
- * This is the method that return a date in a given format.
- * @return the date in a given format.
- */
- public String toString() {
- return MONTHS[month - 1] + " " + day + " , " + year;
- }
- /**
- * This is the method to test our program.
- *
- * @param args
- * we need to check the date.
- */
- public static void main(String[] args) {
- Date date = new Date(2015, 5, 20);
- System.out.println(date);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement