Date.java
Код: Выделить всё
public class Date {
private int year;
private int day;
private int month;
Date(){
}
Date(int year, int day, int month){
this.year = year;
this.month = month;
this.day = day;
}
public int getYear(){
return this.year;
}
public int getMonth(){
return this.month;
}
public int getDayOfMonth() {
return this.day;
}
public String getMonthName(){
return getMonthName(month);
}
public void printShortDate(){
System.out.println(month + "/" + day + "/" + year);
}
public void printLongDate(){
System.out.println(getMonthName(month - 1) + " " + day + ", " + year);
}
#code that wont work
public boolean isLeapYear(){
return isLeapYear(this.year);
}
private int getNumberOfDaysInMonth(int year, int month){
int[] numberOfDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(month == 2 && isLeapYear()){
return 29;
} else {
return numberOfDays[month - 1];
}
}
// private int getNumberOfDaysInYear(int year){
// if (isLeapYear(year)) {
// return 366;
// } else {
// return 365;
// }
// }
private String getMonthName(int month){
String[] monthNames = {"January", "February", "March", "May", "June",
"July", "August", "September", "October", "November", "December"};
return monthNames[month - 1];
}
public void addDays(int days){
while (days != 0){
day++;
if (day > getNumberOfDaysInMonth(year, month)){
month++;
day = 1;
if(month > 12){
month = 1;
day = 1;
year++;
}
}
days--;
}
}
public void subtractDays(int days) {
while (days != 0) {
day--;
if (day == 0) {
if (month == 1) {
month = 12;
day = 31;
year--;
} else {
month--;
}
day = getNumberOfDaysInMonth(year, month);
}
days--;
}
}
}
Код: Выделить всё
public class GregorianDate extends Date {
private int year;
private int month;
private int day;
public GregorianDate(){
this.year = 1970;
this.day = 1;
this.month = 1;
addDays((int)((System.currentTimeMillis()+java.util.TimeZone.getDefault().getRawOffset())/86400000));
}
public GregorianDate(int year, int month, int day){
super();
}
public boolean isLeapYear(int year){
if(year % 4 == 0){
return year % 100 != 0 || (year % 100 == 0 && year % 400 == 0);
}
return false;
}
}
Код: Выделить всё
public class JulianDate extends Date{
private int year;
private int month;
private int day;
public JulianDate(){
this.year = 1;
this.day = 1;
this.month = 1;
addDays(719164);
addDays((int)((System.currentTimeMillis()+java.util.TimeZone.getDefault().getRawOffset())/86400000));
}
public JulianDate(int year, int month, int day){
super();
}
public boolean isLeapYear(int year){
return year % 4 == 0;
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... bstraction