Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Last active September 24, 2019 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/c109c27ca170b7181e70209a1c25cec5 to your computer and use it in GitHub Desktop.
Save bytecodeman/c109c27ca170b7181e70209a1c25cec5 to your computer and use it in GitHub Desktop.
MyDate2.java
// Fixed IncrementDate method
// 10/08/18
import java.util.Calendar;
public class MyDate2 implements Comparable<MyDate2> {
private int month;
private int day;
private int year;
private static Calendar x;
private static int[] monthArr = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static {
x = Calendar.getInstance();
}
// Getters and Setters
public int getMonth() {
return this.month;
}
public int getDay() {
return this.day;
}
public int getYear() {
return this.year;
}
public void setDate(int month, int day, int year) {
setYear(year);
setMonth(month);
setDay(day);
}
private void setYear(int y) {
if (y >= 1)
this.year = y;
else
System.err.println("ERROR: Bad Year Specified: " + y);
}
private void setMonth(int m) {
if (m >= 1 && m <= 12)
this.month = m;
else
System.err.println("ERROR: Bad Month Specified: " + m);
}
private void setDay(int d) {
if (this.month == 2) {
if (d >= 1 && d <= (isLeapYear() ? 29 : 28))
day = d;
else
System.err.println("ERROR: Bad Day Specified: " + d + " for leapyear February");
}
else if (this.month >= 1 && this.month <= 12) {
if (d >= 1 && d <= monthArr[this.month])
this.day = d;
else
System.err.println("ERROR: Bad Day Specified: " + d + " for month: " + month);
}
else {
System.err.println("ERROR: VERY BAD MONTH: " + month);
}
}
// Constructors
public MyDate2(int month, int day, int year) {
setDate(month, day, year);
}
public MyDate2(int month, int year) {
this(month, 1, year);
}
public MyDate2(int year) {
this(1, 1, year);
}
public MyDate2() {
this(x.get(Calendar.MONTH) + 1, x.get(Calendar.DATE), x.get(Calendar.YEAR));
}
// Utility Methods
public String toString() {
return String.format("%02d/%02d/%02d", this.month, this.day, this.year);
}
public boolean isLeapYear() {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
public int getDaysInMonth() {
int noOfDays = 0;
if (this.month >= 1 && this.month <= 12) {
noOfDays = monthArr[this.month];
if (this.month == 2 && isLeapYear())
noOfDays = 29;
}
return noOfDays;
}
int numberOfDaysLeft() {
if (isLeapYear())
return 366 - numberOfDaysPassed();
else
return 365 - numberOfDaysPassed();
}
public int numberOfDaysPassed() {
int sumDays = 0;
for (int i = 1; i < this.month; i++)
sumDays += monthArr[i];
if (isLeapYear() && this.month > 2)
sumDays += this.day + 1;
else
sumDays += this.day;
return sumDays;
}
public void incrementDate(int nDays) {
for (int i = 0; i < nDays; i++) {
this.day++;
if (this.day > monthArr[this.month] + (this.isLeapYear() ? 1 : 0)) {
this.day = 1;
this.month++;
if (this.month > 12) {
this.month = 1;
this.year++;
}
}
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + day;
result = prime * result + month;
result = prime * result + year;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyDate2 other = (MyDate2) obj;
if (day != other.day)
return false;
if (month != other.month)
return false;
if (year != other.year)
return false;
return true;
}
public int compareTo(MyDate2 other) {
if (this.year != other.year)
return this.year - other.year;
if (this.month != other.month)
return this.month - other.month;
return this.day - other.day;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment