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/c06174fd99c4d24807ab8b0ec69f39ca to your computer and use it in GitHub Desktop.
Save bytecodeman/c06174fd99c4d24807ab8b0ec69f39ca to your computer and use it in GitHub Desktop.
MyDate.java
// Fixed IncrementDate method
// 10/08/18
import java.util.Calendar;
public class MyDate extends Object {
private int month;
private int day;
private int year;
// Getters and Setters
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() {
return year;
}
public void setDate(int month, int day, int year) {
setYear(year);
setMonth(month);
setDay(day);
}
private void setYear(int y) {
if (y >= 1)
year = y;
else
System.err.println("ERROR: Bad Year Specified: " + y);
}
private void setMonth(int m) {
if (m >= 1 && m <= 12)
month = m;
else
System.err.println("ERROR: Bad Month Specified: " + m);
}
private void setDay(int d) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (d >= 1 && d <= 31)
day = d;
else
System.err.println("ERROR: Bad Day Specified: " + d
+ " for month: " + month);
break;
case 4:
case 6:
case 9:
case 11:
if (d >= 1 && d <= 30)
day = d;
else
System.err.println("ERROR: Bad Day Specified: " + d
+ " for month: " + month);
break;
case 2:
if (this.isLeapYear())
if (d >= 1 && d <= 29)
day = d;
else
System.err.println("ERROR: Bad Day Specified: " + d
+ " for leapyear February");
else if (d >= 1 && d <= 28)
day = d;
else
System.err.println("ERROR: Bad Day Specified: " + d
+ " for leapyear February");
break;
default:
System.err.println("ERROR: VERY BAD MONTH: " + month);
break;
}
}
// Constructors
public MyDate(int month, int day, int year) {
this.setDate(month, day, year);
}
public MyDate() {
Calendar x = Calendar.getInstance();
month = x.get(Calendar.MONTH) + 1;
day = x.get(Calendar.DATE);
year = x.get(Calendar.YEAR);
}
public MyDate(int month, int year) {
this.setDate(month, 1, year);
}
public MyDate(int year) {
this.setDate(1, 1, year);
}
// Utility Methods
private static int getDaysInMonth(int month, boolean isLeap) {
int noOfDays = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
noOfDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
noOfDays = 30;
break;
case 2:
if (isLeap)
noOfDays = 29;
else
noOfDays = 28;
break;
}
return noOfDays;
}
public int getDaysInMonth() {
return getDaysInMonth(this.month, this.isLeapYear());
}
public int numberOfDaysLeft() {
if (isLeapYear())
return 366 - numberOfDaysPassed();
else
return 365 - numberOfDaysPassed();
}
public int numberOfDaysPassed() {
int sumDays = 0;
for (int i = 1; i < month; i++)
sumDays += getDaysInMonth(i, isLeapYear());
sumDays += day;
return sumDays;
}
public void incrementDate(int nDays) {
for (int i = 0; i < nDays; i++) {
this.day++;
if (this.day > getDaysInMonth(this.month, this.isLeapYear())) {
this.day = 1;
this.month++;
if (this.month > 12) {
this.month = 1;
this.year++;
}
}
}
}
public boolean isLeapYear() {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
public String toString() {
return String.format("%02d/%02d/%02d", month, day, year);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment