Created
January 18, 2019 19:19
-
-
Save TGNYC/c1e49b738316e6062260738b7f1812d7 to your computer and use it in GitHub Desktop.
Date Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package dateclass; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Calendar; | |
| import java.time.LocalDate; | |
| import java.time.temporal.ChronoUnit; | |
| /** | |
| * @author Tejas Gupta | |
| * (c) 2016 | |
| */ | |
| public class Date implements Comparable<Date> | |
| { | |
| private int month, day, year; | |
| private String dayOfWeek; | |
| private final String[] months | |
| = {"January", | |
| "February", | |
| "March", | |
| "April", | |
| "May", | |
| "June", | |
| "July", | |
| "August", | |
| "September", | |
| "October", | |
| "November", | |
| "December"}; | |
| // CONSTRUCTOR | |
| public Date(int month, int day, int year) throws IllegalArgumentException | |
| { | |
| if (this.validateMonth(month) == true) | |
| { this.month = month; } | |
| else { throw new IllegalArgumentException("Invalid month."); } | |
| if (this.validateDay(month, day) == true) | |
| { this.day = day; } | |
| else { throw new IllegalArgumentException("Invalid day."); } | |
| this.year = year; | |
| this.dayOfWeek = Date.getDayOfWeek(this); | |
| } | |
| // DAYS BETWEEN | |
| public int daysBetween(Date a, Date b) | |
| { | |
| LocalDate startDate = LocalDate.parse(Date.getAbbreviatedDate(a)); | |
| LocalDate endDate = LocalDate.parse(Date.getAbbreviatedDate(b)); | |
| Long range = ChronoUnit.DAYS.between(startDate, endDate); | |
| return Math.toIntExact(range); | |
| } | |
| // VALIDATIONS | |
| public static boolean validateMonth(int month) | |
| { | |
| if (1 <= month && month <= 12) | |
| { return true; } | |
| else | |
| { return false; } | |
| } | |
| public static boolean validateDay(int month, int day) | |
| { | |
| boolean output = false; | |
| switch (month) | |
| { | |
| case 1: if (1 <= day && day <= 31) | |
| { return true; } | |
| break; | |
| case 2: if (1 <= day && day <= 29) | |
| { return true; } | |
| break; | |
| case 3: if (1 <= day && day <= 31) | |
| { return true; } | |
| break; | |
| case 4: if (1 <= day && day <= 30) | |
| { return true; } | |
| break; | |
| case 5: if (1 <= day && day <= 31) | |
| { return true; } | |
| break; | |
| case 6: if (1 <= day && day <= 30) | |
| { return true; } | |
| break; | |
| case 7: if (1 <= day && day <= 31) | |
| { return true; } | |
| break; | |
| case 8: if (1 <= day && day <= 31) | |
| { return true; } | |
| break; | |
| case 9: if (1 <= day && day <= 30) | |
| { return true; } | |
| break; | |
| case 10: if (1 <= day && day <= 31) | |
| { return true; } | |
| break; | |
| case 11: if (1 <= day && day <= 30) | |
| { return true; } | |
| break; | |
| case 12: if (1 <= day && day <= 31) | |
| { return true; } | |
| break; | |
| default: break; | |
| } | |
| return output; | |
| } | |
| public static boolean validateDate(Date a) | |
| { | |
| boolean output = false; | |
| if (Date.validateMonth(a.getMonth()) == true) | |
| { | |
| if (Date.validateDay(a.getMonth(), a.getDay())) | |
| { | |
| output = true; | |
| } | |
| } | |
| return output; | |
| } | |
| // TOSTRING | |
| @Override | |
| public String toString() | |
| { | |
| // GET MONTH STRING | |
| String monthString = months[this.getMonth()-1]; | |
| String output = "This date is " + Date.getDayOfWeek(this) + ", " + | |
| monthString + " " + this.day + ", " + this.year; | |
| return output; | |
| } | |
| // COMPARISON | |
| @Override | |
| public int compareTo(Date o) | |
| { | |
| if (this.daysBetween(this, o) > 0) | |
| { return 1; } | |
| else if (this.daysBetween(this, o) < 0) | |
| { return -1; } | |
| else | |
| { return 0; } | |
| } | |
| public boolean equals(Date o) | |
| { | |
| if (Date.getAbbreviatedDate(this).equals(Date.getAbbreviatedDate(o))) | |
| { return true; } | |
| else { return false; } | |
| } | |
| // SETS AND GETS | |
| public static String getDayOfWeek(Date a) | |
| { | |
| Calendar c = Calendar.getInstance(); | |
| c.set(a.getDay(), a.getMonth(), a.getYear()); | |
| int day_of_week = c.get(Calendar.DAY_OF_WEEK); | |
| String output = "Monday"; | |
| switch (day_of_week) | |
| { | |
| case 1: output = "Sunday"; | |
| case 2: output = "Monday"; | |
| case 3: output = "Tuesday"; | |
| case 4: output = "Wednesday"; | |
| case 5: output = "Thursday"; | |
| case 6: output = "Friday"; | |
| case 7: output = "Saturday"; | |
| default: output = "Monday"; | |
| } | |
| return output; | |
| } | |
| public int getMonth() { | |
| return month; | |
| } | |
| public void setMonth(int month) throws IllegalArgumentException { | |
| if (this.validateMonth(month) == true) | |
| { this.month = month; } | |
| else | |
| { throw new IllegalArgumentException("Invalid month."); } | |
| } | |
| public int getDay() { | |
| return day; | |
| } | |
| public void setDay(int day) throws IllegalArgumentException { | |
| if (this.validateDay(this.month, day) == true) | |
| { this.day = day; } | |
| else { throw new IllegalArgumentException("Invalid day."); } | |
| } | |
| public int getYear() { | |
| return year; | |
| } | |
| public void setYear(int year) { | |
| this.year = year; | |
| } | |
| public static String getAbbreviatedDate(Date a) | |
| { | |
| String month = Integer.toString(a.getMonth()); | |
| String day = Integer.toString(a.getDay()); | |
| String year = Integer.toString(a.getYear()); | |
| String date = year + "-" + month + "-" + day; | |
| return date; | |
| } | |
| public static String getAbbreviatedDate(int m, int d, int y) | |
| { | |
| String month = Integer.toString(m); | |
| String day = Integer.toString(d); | |
| String year = Integer.toString(y); | |
| String date = year + "-" + month + "-" + day; | |
| return date; | |
| } | |
| public static Date getCurrentDate() | |
| { | |
| String test = new SimpleDateFormat("MM/dd/yyyy").format(Calendar.getInstance().getTime()); | |
| int month = Integer.parseInt(test.substring(0, 2)); | |
| int day = Integer.parseInt(test.substrring(3, 5)); | |
| int year = Integer.parseInt(test.substring(6)); | |
| Date currentDate = new Date(month, day, year); | |
| return currentDate; | |
| } | |
| public void addDay() { this.day++; } | |
| public void subtractday() { this.day--; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment