SlideShare a Scribd company logo
publicclass Date {
privatestatic String DATE_SEPARATOR = "/";
privatestaticintDAYS_PER_WEEK = 7;
//Attributes
privateint day;
privateint month;
privateint year;
/**
* Default Constructor
* Instantiates an object of type Date to 1/1/2000
*/
public Date() {
this.day = 1;
this.month = 1;
this.year = 2000;
}
/**
* Constructs a new date object to represent the given date.
* @param day
* @param month
* @param year
*/
public Date(int day, int month, int year) {
if(isValid(day, month, year)) {
this.day = day;
this.month = month;
this.year = year;
} else
System.out.println("Invalid Date.");
}
/**
* Returns the day value of this date for example, for the date 2006/07/22, returns 22.
* @return
*/
publicint getDay() {
return day;
}
/**
* Returns the month value of this date ,for example, for the date 2006/07/22, returns 7.
* @return
*/
publicint getMonth() {
return month;
}
/**
* Returns the year value of this date, for example , the date 2006/07/22, returns 2006.
* @return
*/
publicint getYear() {
return year;
}
/**
* @param day the day to set
*/
publicvoid setDay(int day) {
this.day = day;
}
/**
* @param month the month to set
*/
publicvoid setMonth(int month) {
this.month = month;
}
/**
* @param year the year to set
*/
publicvoid setYear(int year) {
this.year = year;
}
/**
* Returns true if the year of this date is a leap year.
* A leap year occurs every 4 years , except for multiples of 100 that are not multiples of 400.
* For example, 1956,1844,1600,and 2000 are leap years, but 1983,2002,1700,and 1900 are not.
* @return
*/
publicboolean isLeapYear(){
if(((this.year % 400) == 0) || (((this.year % 4) == 0) && ((this.year % 100) != 0)))
returntrue;
else
returnfalse;
}
/**
* Checks if the date is valid
* @param day
* @param month
* @param year
* @return
*/
publicboolean isValid(int day, int month, int year) {
if((month < 1) || (12 < month))
returnfalse;
else {
if(((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
(month == 8) || (month == 10) || (month == 12)) && ((day < 1) || (31 < day)))
returnfalse;
elseif(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && ((day < 1) || (30 <
day)))
returnfalse;
elseif(month == 2) {
if(isLeapYear() && ((day < 1) || (29 < day)))
returnfalse;
elseif((day < 1) || (28 < day))
returnfalse;
}
}
returntrue;
}
/**
* Returns the maximum number of days in a month
* @return
*/
publicint maxMonthDays() {
if(this.month == 2) {
if(isLeapYear())
return 29;
else
return 28;
} elseif((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) ||
(this.month == 8) || (this.month == 10) || (this.month == 12))
return 31;
else
return 30;
}
/**
* Checks if this dat is same as other date
* @param other
* @return
*/
publicboolean isEqual(Date other) {
if((this.day == other.day) && (this.month == other.month) && (this.year == other.year))
returntrue;
else
returnfalse;
}
/**
* Moves this Date object forward in time by the given number of days .
* @param days
*/
publicvoid addDays(int days) {
while(days != 0){
int maxDays = maxMonthDays();
if((maxDays - this.day) >= days) {
this.day += days;
days = 0;
}
else{
days -= (maxDays - this.day);
this.day = 0;
if(month == 12) {
this.month = 1;
this.year += 1;
} else
this.month += 1;
}
}
}
/**
* Moves this date object forward in time by the given amount of seven day weeks
* @param weeks
*/
publicvoid addWeeks(int weeks) {
addDays(weeks * DAYS_PER_WEEK);
}
/**
* Returns the number of days that this Date must be adjusted to make it equal to the given other
Date.
* @param other
* @return
*/
publicint daysTo(Date other) {
Date temp = new Date(this.day, this.month, this.year);
if(temp.isEqual(other))
return 0;
else{
int noOfDays = 0;
do {
if((temp.year == other.year) && (temp.month == other.month)) {
noOfDays += (other.day - temp.day);
temp.day = other.day;
}
else{
int maxDays = temp.maxMonthDays();
noOfDays += (maxDays - temp.day);
temp.day = 0;
if(temp.month == 12) {
temp.month = 1;
temp.year += 1;
}
else
temp.month += 1;
}
} while(!temp.isEqual(other));
return noOfDays;
}
}
/**
* Returns a String representation of this date in year/month/day order, such as "2006/07/22"
*/
@Override
public String toString() {
returnthis.day + DATE_SEPARATOR + this.month + DATE_SEPARATOR + this.year;
}
}
publicclass Driver {
publicstaticvoid main(String[] args) {
Date d1 = new Date(29, 1, 2016);
Date d2 = new Date(29, 12, 2015);
//Add 5 days to d1
System.out.println(" Add 5 days to " + d1 + " : ");
d1.addDays(5);
System.out.println(d1);
//Add 5 days to d2
System.out.println(" Add 5 days to " + d2 + " : ");
d2.addDays(5);
System.out.println(d2);
Date d3 = new Date(28, 8, 2016);
//Add 1 week to d3
System.out.println(" Add 1 week to " + d3 + " : ");
d3.addWeeks(1);
System.out.println(d3);
Date d4 = new Date(31, 8, 2016);
Date d5 = new Date(1, 10, 2016);
System.out.println(" The number of days that " + d4 + " must be adjusted to make it equal to
" + d5 + " : " + d4.daysTo(d5) + " days.");
}
}
SAMPLE OUTPUT :
Add 5 days to 29/1/2016 :
3/2/2016
Add 5 days to 29/12/2015 :
3/1/2016
Add 1 week to 28/8/2016 :
4/9/2016
The number of days that 31/8/2016 must be adjusted to make it equal to 1/10/2016 : 31 days.
Solution
publicclass Date {
privatestatic String DATE_SEPARATOR = "/";
privatestaticintDAYS_PER_WEEK = 7;
//Attributes
privateint day;
privateint month;
privateint year;
/**
* Default Constructor
* Instantiates an object of type Date to 1/1/2000
*/
public Date() {
this.day = 1;
this.month = 1;
this.year = 2000;
}
/**
* Constructs a new date object to represent the given date.
* @param day
* @param month
* @param year
*/
public Date(int day, int month, int year) {
if(isValid(day, month, year)) {
this.day = day;
this.month = month;
this.year = year;
} else
System.out.println("Invalid Date.");
}
/**
* Returns the day value of this date for example, for the date 2006/07/22, returns 22.
* @return
*/
publicint getDay() {
return day;
}
/**
* Returns the month value of this date ,for example, for the date 2006/07/22, returns 7.
* @return
*/
publicint getMonth() {
return month;
}
/**
* Returns the year value of this date, for example , the date 2006/07/22, returns 2006.
* @return
*/
publicint getYear() {
return year;
}
/**
* @param day the day to set
*/
publicvoid setDay(int day) {
this.day = day;
}
/**
* @param month the month to set
*/
publicvoid setMonth(int month) {
this.month = month;
}
/**
* @param year the year to set
*/
publicvoid setYear(int year) {
this.year = year;
}
/**
* Returns true if the year of this date is a leap year.
* A leap year occurs every 4 years , except for multiples of 100 that are not multiples of 400.
* For example, 1956,1844,1600,and 2000 are leap years, but 1983,2002,1700,and 1900 are not.
* @return
*/
publicboolean isLeapYear(){
if(((this.year % 400) == 0) || (((this.year % 4) == 0) && ((this.year % 100) != 0)))
returntrue;
else
returnfalse;
}
/**
* Checks if the date is valid
* @param day
* @param month
* @param year
* @return
*/
publicboolean isValid(int day, int month, int year) {
if((month < 1) || (12 < month))
returnfalse;
else {
if(((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
(month == 8) || (month == 10) || (month == 12)) && ((day < 1) || (31 < day)))
returnfalse;
elseif(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && ((day < 1) || (30 <
day)))
returnfalse;
elseif(month == 2) {
if(isLeapYear() && ((day < 1) || (29 < day)))
returnfalse;
elseif((day < 1) || (28 < day))
returnfalse;
}
}
returntrue;
}
/**
* Returns the maximum number of days in a month
* @return
*/
publicint maxMonthDays() {
if(this.month == 2) {
if(isLeapYear())
return 29;
else
return 28;
} elseif((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) ||
(this.month == 8) || (this.month == 10) || (this.month == 12))
return 31;
else
return 30;
}
/**
* Checks if this dat is same as other date
* @param other
* @return
*/
publicboolean isEqual(Date other) {
if((this.day == other.day) && (this.month == other.month) && (this.year == other.year))
returntrue;
else
returnfalse;
}
/**
* Moves this Date object forward in time by the given number of days .
* @param days
*/
publicvoid addDays(int days) {
while(days != 0){
int maxDays = maxMonthDays();
if((maxDays - this.day) >= days) {
this.day += days;
days = 0;
}
else{
days -= (maxDays - this.day);
this.day = 0;
if(month == 12) {
this.month = 1;
this.year += 1;
} else
this.month += 1;
}
}
}
/**
* Moves this date object forward in time by the given amount of seven day weeks
* @param weeks
*/
publicvoid addWeeks(int weeks) {
addDays(weeks * DAYS_PER_WEEK);
}
/**
* Returns the number of days that this Date must be adjusted to make it equal to the given other
Date.
* @param other
* @return
*/
publicint daysTo(Date other) {
Date temp = new Date(this.day, this.month, this.year);
if(temp.isEqual(other))
return 0;
else{
int noOfDays = 0;
do {
if((temp.year == other.year) && (temp.month == other.month)) {
noOfDays += (other.day - temp.day);
temp.day = other.day;
}
else{
int maxDays = temp.maxMonthDays();
noOfDays += (maxDays - temp.day);
temp.day = 0;
if(temp.month == 12) {
temp.month = 1;
temp.year += 1;
}
else
temp.month += 1;
}
} while(!temp.isEqual(other));
return noOfDays;
}
}
/**
* Returns a String representation of this date in year/month/day order, such as "2006/07/22"
*/
@Override
public String toString() {
returnthis.day + DATE_SEPARATOR + this.month + DATE_SEPARATOR + this.year;
}
}
publicclass Driver {
publicstaticvoid main(String[] args) {
Date d1 = new Date(29, 1, 2016);
Date d2 = new Date(29, 12, 2015);
//Add 5 days to d1
System.out.println(" Add 5 days to " + d1 + " : ");
d1.addDays(5);
System.out.println(d1);
//Add 5 days to d2
System.out.println(" Add 5 days to " + d2 + " : ");
d2.addDays(5);
System.out.println(d2);
Date d3 = new Date(28, 8, 2016);
//Add 1 week to d3
System.out.println(" Add 1 week to " + d3 + " : ");
d3.addWeeks(1);
System.out.println(d3);
Date d4 = new Date(31, 8, 2016);
Date d5 = new Date(1, 10, 2016);
System.out.println(" The number of days that " + d4 + " must be adjusted to make it equal to
" + d5 + " : " + d4.daysTo(d5) + " days.");
}
}
SAMPLE OUTPUT :
Add 5 days to 29/1/2016 :
3/2/2016
Add 5 days to 29/12/2015 :
3/1/2016
Add 1 week to 28/8/2016 :
4/9/2016
The number of days that 31/8/2016 must be adjusted to make it equal to 1/10/2016 : 31 days.

More Related Content

PDF
Date class that represents a date consisting of a year, month, and a.pdf
PDF
Hi,I have implemented increment() method. Please find the below up.pdf
PDF
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
PDF
Modify the Date class that was covered in the lecture which overload.pdf
PDF
Date.h#ifndef DateFormat#define DateFormat#includetime.h.pdf
PDF
Manipulating data with dates
PDF
Problem DefinitionBuild a Date class and a main function to test i.pdf
PDF
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
Date class that represents a date consisting of a year, month, and a.pdf
Hi,I have implemented increment() method. Please find the below up.pdf
CounterTest.javaimport static org.junit.Assert.;import org.jun.pdf
Modify the Date class that was covered in the lecture which overload.pdf
Date.h#ifndef DateFormat#define DateFormat#includetime.h.pdf
Manipulating data with dates
Problem DefinitionBuild a Date class and a main function to test i.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdf

Similar to publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf (20)

PDF
Write a program to generate the entire calendar for one year. The pr.pdf
PDF
ThreeTen
PDF
@author Haolin Jin To generate weather for locatio.pdf
PDF
Assignment Details There is a .h file on Moodle that provides a defi.pdf
DOCX
Script Files
PPTX
Date time function in Database
PPTX
datetimefuction-170413055211.pptx
PDF
Please I am posting the fifth time and hoping to get this r.pdf
DOCX
Q2dayOfYear.cppQ2dayOfYear.cpp  Name   Copyright  .docx
PDF
C++ Please I am posting the fifth time and hoping to get th.pdf
PPTX
Synapse india dotnet development overloading operater part 4
PDF
Need to make a Java program which calculates the number of days betw.pdf
PDF
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
PPT
Dynamically Evolving Systems: Cluster Analysis Using Time
DOCX
In this assignment, you will continue working on your application..docx
PDF
Functional C++
DOCX
Builtinfunctions in vbscript and its types.docx
PDF
DO NOT use System.exit().DO NOT add the project or package stateme.pdf
PDF
Jsr310
TXT
Data20161007
Write a program to generate the entire calendar for one year. The pr.pdf
ThreeTen
@author Haolin Jin To generate weather for locatio.pdf
Assignment Details There is a .h file on Moodle that provides a defi.pdf
Script Files
Date time function in Database
datetimefuction-170413055211.pptx
Please I am posting the fifth time and hoping to get this r.pdf
Q2dayOfYear.cppQ2dayOfYear.cpp  Name   Copyright  .docx
C++ Please I am posting the fifth time and hoping to get th.pdf
Synapse india dotnet development overloading operater part 4
Need to make a Java program which calculates the number of days betw.pdf
Date and Timestamp Types In Snowflake (By Faysal Shaarani)
Dynamically Evolving Systems: Cluster Analysis Using Time
In this assignment, you will continue working on your application..docx
Functional C++
Builtinfunctions in vbscript and its types.docx
DO NOT use System.exit().DO NOT add the project or package stateme.pdf
Jsr310
Data20161007

More from mukhtaransarcloth (20)

PDF
S monoclinic is most stable since entropy is the .pdf
PDF
Option D is correct. B is limiting reagent .pdf
PDF
Let us assume the conc of Ba(OH)2 is known to us .pdf
PDF
london dispersion forces .pdf
PDF
Methods a, b, c, and e are formal additions of H2.pdf
PDF
h2s4 structure is H-S-S-S-S-H there for two middl.pdf
PDF
freezing point decreases with increase in imp.pdf
PDF
We are currently living in the so-called information age which can b.pdf
PDF
The balanced equation is2 H2O2 = 2 H2O + O2SolutionThe bal.pdf
PDF
Tay-Sachs disease is caused by a mutation (abnormal change) in the g.pdf
PDF
E.) is less acidic in other A ,B triple bond an.pdf
PDF
Solution when user sending the email, then user should select eithe.pdf
PDF
please give me points as nobody ecen noticed the qusetion as it is u.pdf
PDF
covalent bond .pdf
PDF
Part-IQuestion 1. What is Dr. Warren’s hypothesis regarding the ba.pdf
PDF
Peru is not a part of the Southern South America.SolutionPeru .pdf
PDF
Combustion of glucose is respiration. The equatio.pdf
PDF
in truss one...the forces in the member BC and DE have zero.in tru.pdf
PDF
iam giving you entire process of  forensc duplication;the response.pdf
PDF
How does traffic analysis work Internet data packets have two parts.pdf
S monoclinic is most stable since entropy is the .pdf
Option D is correct. B is limiting reagent .pdf
Let us assume the conc of Ba(OH)2 is known to us .pdf
london dispersion forces .pdf
Methods a, b, c, and e are formal additions of H2.pdf
h2s4 structure is H-S-S-S-S-H there for two middl.pdf
freezing point decreases with increase in imp.pdf
We are currently living in the so-called information age which can b.pdf
The balanced equation is2 H2O2 = 2 H2O + O2SolutionThe bal.pdf
Tay-Sachs disease is caused by a mutation (abnormal change) in the g.pdf
E.) is less acidic in other A ,B triple bond an.pdf
Solution when user sending the email, then user should select eithe.pdf
please give me points as nobody ecen noticed the qusetion as it is u.pdf
covalent bond .pdf
Part-IQuestion 1. What is Dr. Warren’s hypothesis regarding the ba.pdf
Peru is not a part of the Southern South America.SolutionPeru .pdf
Combustion of glucose is respiration. The equatio.pdf
in truss one...the forces in the member BC and DE have zero.in tru.pdf
iam giving you entire process of  forensc duplication;the response.pdf
How does traffic analysis work Internet data packets have two parts.pdf

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
master seminar digital applications in india
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
RMMM.pdf make it easy to upload and study
PDF
Classroom Observation Tools for Teachers
PDF
01-Introduction-to-Information-Management.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Pharmacology of Heart Failure /Pharmacotherapy of CHF
master seminar digital applications in india
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
RMMM.pdf make it easy to upload and study
Classroom Observation Tools for Teachers
01-Introduction-to-Information-Management.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Module 4: Burden of Disease Tutorial Slides S2 2025
Abdominal Access Techniques with Prof. Dr. R K Mishra
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
human mycosis Human fungal infections are called human mycosis..pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx

publicclass Date {privatestatic String DATE_SEPARATOR = ;pr.pdf

  • 1. publicclass Date { privatestatic String DATE_SEPARATOR = "/"; privatestaticintDAYS_PER_WEEK = 7; //Attributes privateint day; privateint month; privateint year; /** * Default Constructor * Instantiates an object of type Date to 1/1/2000 */ public Date() { this.day = 1; this.month = 1; this.year = 2000; } /** * Constructs a new date object to represent the given date. * @param day * @param month * @param year */ public Date(int day, int month, int year) { if(isValid(day, month, year)) { this.day = day; this.month = month; this.year = year; } else System.out.println("Invalid Date."); } /** * Returns the day value of this date for example, for the date 2006/07/22, returns 22. * @return */ publicint getDay() {
  • 2. return day; } /** * Returns the month value of this date ,for example, for the date 2006/07/22, returns 7. * @return */ publicint getMonth() { return month; } /** * Returns the year value of this date, for example , the date 2006/07/22, returns 2006. * @return */ publicint getYear() { return year; } /** * @param day the day to set */ publicvoid setDay(int day) { this.day = day; } /** * @param month the month to set */ publicvoid setMonth(int month) { this.month = month; } /** * @param year the year to set */ publicvoid setYear(int year) { this.year = year; } /** * Returns true if the year of this date is a leap year.
  • 3. * A leap year occurs every 4 years , except for multiples of 100 that are not multiples of 400. * For example, 1956,1844,1600,and 2000 are leap years, but 1983,2002,1700,and 1900 are not. * @return */ publicboolean isLeapYear(){ if(((this.year % 400) == 0) || (((this.year % 4) == 0) && ((this.year % 100) != 0))) returntrue; else returnfalse; } /** * Checks if the date is valid * @param day * @param month * @param year * @return */ publicboolean isValid(int day, int month, int year) { if((month < 1) || (12 < month)) returnfalse; else { if(((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && ((day < 1) || (31 < day))) returnfalse; elseif(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && ((day < 1) || (30 < day))) returnfalse; elseif(month == 2) { if(isLeapYear() && ((day < 1) || (29 < day))) returnfalse; elseif((day < 1) || (28 < day)) returnfalse; } } returntrue; }
  • 4. /** * Returns the maximum number of days in a month * @return */ publicint maxMonthDays() { if(this.month == 2) { if(isLeapYear()) return 29; else return 28; } elseif((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) || (this.month == 8) || (this.month == 10) || (this.month == 12)) return 31; else return 30; } /** * Checks if this dat is same as other date * @param other * @return */ publicboolean isEqual(Date other) { if((this.day == other.day) && (this.month == other.month) && (this.year == other.year)) returntrue; else returnfalse; } /** * Moves this Date object forward in time by the given number of days . * @param days */ publicvoid addDays(int days) { while(days != 0){ int maxDays = maxMonthDays(); if((maxDays - this.day) >= days) { this.day += days;
  • 5. days = 0; } else{ days -= (maxDays - this.day); this.day = 0; if(month == 12) { this.month = 1; this.year += 1; } else this.month += 1; } } } /** * Moves this date object forward in time by the given amount of seven day weeks * @param weeks */ publicvoid addWeeks(int weeks) { addDays(weeks * DAYS_PER_WEEK); } /** * Returns the number of days that this Date must be adjusted to make it equal to the given other Date. * @param other * @return */ publicint daysTo(Date other) { Date temp = new Date(this.day, this.month, this.year); if(temp.isEqual(other)) return 0; else{ int noOfDays = 0; do { if((temp.year == other.year) && (temp.month == other.month)) { noOfDays += (other.day - temp.day); temp.day = other.day;
  • 6. } else{ int maxDays = temp.maxMonthDays(); noOfDays += (maxDays - temp.day); temp.day = 0; if(temp.month == 12) { temp.month = 1; temp.year += 1; } else temp.month += 1; } } while(!temp.isEqual(other)); return noOfDays; } } /** * Returns a String representation of this date in year/month/day order, such as "2006/07/22" */ @Override public String toString() { returnthis.day + DATE_SEPARATOR + this.month + DATE_SEPARATOR + this.year; } } publicclass Driver { publicstaticvoid main(String[] args) { Date d1 = new Date(29, 1, 2016); Date d2 = new Date(29, 12, 2015); //Add 5 days to d1 System.out.println(" Add 5 days to " + d1 + " : "); d1.addDays(5); System.out.println(d1); //Add 5 days to d2 System.out.println(" Add 5 days to " + d2 + " : "); d2.addDays(5); System.out.println(d2);
  • 7. Date d3 = new Date(28, 8, 2016); //Add 1 week to d3 System.out.println(" Add 1 week to " + d3 + " : "); d3.addWeeks(1); System.out.println(d3); Date d4 = new Date(31, 8, 2016); Date d5 = new Date(1, 10, 2016); System.out.println(" The number of days that " + d4 + " must be adjusted to make it equal to " + d5 + " : " + d4.daysTo(d5) + " days."); } } SAMPLE OUTPUT : Add 5 days to 29/1/2016 : 3/2/2016 Add 5 days to 29/12/2015 : 3/1/2016 Add 1 week to 28/8/2016 : 4/9/2016 The number of days that 31/8/2016 must be adjusted to make it equal to 1/10/2016 : 31 days. Solution publicclass Date { privatestatic String DATE_SEPARATOR = "/"; privatestaticintDAYS_PER_WEEK = 7; //Attributes privateint day; privateint month; privateint year; /** * Default Constructor * Instantiates an object of type Date to 1/1/2000 */ public Date() { this.day = 1; this.month = 1;
  • 8. this.year = 2000; } /** * Constructs a new date object to represent the given date. * @param day * @param month * @param year */ public Date(int day, int month, int year) { if(isValid(day, month, year)) { this.day = day; this.month = month; this.year = year; } else System.out.println("Invalid Date."); } /** * Returns the day value of this date for example, for the date 2006/07/22, returns 22. * @return */ publicint getDay() { return day; } /** * Returns the month value of this date ,for example, for the date 2006/07/22, returns 7. * @return */ publicint getMonth() { return month; } /** * Returns the year value of this date, for example , the date 2006/07/22, returns 2006. * @return */ publicint getYear() { return year;
  • 9. } /** * @param day the day to set */ publicvoid setDay(int day) { this.day = day; } /** * @param month the month to set */ publicvoid setMonth(int month) { this.month = month; } /** * @param year the year to set */ publicvoid setYear(int year) { this.year = year; } /** * Returns true if the year of this date is a leap year. * A leap year occurs every 4 years , except for multiples of 100 that are not multiples of 400. * For example, 1956,1844,1600,and 2000 are leap years, but 1983,2002,1700,and 1900 are not. * @return */ publicboolean isLeapYear(){ if(((this.year % 400) == 0) || (((this.year % 4) == 0) && ((this.year % 100) != 0))) returntrue; else returnfalse; } /** * Checks if the date is valid * @param day * @param month * @param year
  • 10. * @return */ publicboolean isValid(int day, int month, int year) { if((month < 1) || (12 < month)) returnfalse; else { if(((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && ((day < 1) || (31 < day))) returnfalse; elseif(((month == 4) || (month == 6) || (month == 9) || (month == 11)) && ((day < 1) || (30 < day))) returnfalse; elseif(month == 2) { if(isLeapYear() && ((day < 1) || (29 < day))) returnfalse; elseif((day < 1) || (28 < day)) returnfalse; } } returntrue; } /** * Returns the maximum number of days in a month * @return */ publicint maxMonthDays() { if(this.month == 2) { if(isLeapYear()) return 29; else return 28; } elseif((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) || (this.month == 8) || (this.month == 10) || (this.month == 12)) return 31; else return 30;
  • 11. } /** * Checks if this dat is same as other date * @param other * @return */ publicboolean isEqual(Date other) { if((this.day == other.day) && (this.month == other.month) && (this.year == other.year)) returntrue; else returnfalse; } /** * Moves this Date object forward in time by the given number of days . * @param days */ publicvoid addDays(int days) { while(days != 0){ int maxDays = maxMonthDays(); if((maxDays - this.day) >= days) { this.day += days; days = 0; } else{ days -= (maxDays - this.day); this.day = 0; if(month == 12) { this.month = 1; this.year += 1; } else this.month += 1; } } } /** * Moves this date object forward in time by the given amount of seven day weeks
  • 12. * @param weeks */ publicvoid addWeeks(int weeks) { addDays(weeks * DAYS_PER_WEEK); } /** * Returns the number of days that this Date must be adjusted to make it equal to the given other Date. * @param other * @return */ publicint daysTo(Date other) { Date temp = new Date(this.day, this.month, this.year); if(temp.isEqual(other)) return 0; else{ int noOfDays = 0; do { if((temp.year == other.year) && (temp.month == other.month)) { noOfDays += (other.day - temp.day); temp.day = other.day; } else{ int maxDays = temp.maxMonthDays(); noOfDays += (maxDays - temp.day); temp.day = 0; if(temp.month == 12) { temp.month = 1; temp.year += 1; } else temp.month += 1; } } while(!temp.isEqual(other)); return noOfDays; }
  • 13. } /** * Returns a String representation of this date in year/month/day order, such as "2006/07/22" */ @Override public String toString() { returnthis.day + DATE_SEPARATOR + this.month + DATE_SEPARATOR + this.year; } } publicclass Driver { publicstaticvoid main(String[] args) { Date d1 = new Date(29, 1, 2016); Date d2 = new Date(29, 12, 2015); //Add 5 days to d1 System.out.println(" Add 5 days to " + d1 + " : "); d1.addDays(5); System.out.println(d1); //Add 5 days to d2 System.out.println(" Add 5 days to " + d2 + " : "); d2.addDays(5); System.out.println(d2); Date d3 = new Date(28, 8, 2016); //Add 1 week to d3 System.out.println(" Add 1 week to " + d3 + " : "); d3.addWeeks(1); System.out.println(d3); Date d4 = new Date(31, 8, 2016); Date d5 = new Date(1, 10, 2016); System.out.println(" The number of days that " + d4 + " must be adjusted to make it equal to " + d5 + " : " + d4.daysTo(d5) + " days."); } } SAMPLE OUTPUT : Add 5 days to 29/1/2016 : 3/2/2016 Add 5 days to 29/12/2015 :
  • 14. 3/1/2016 Add 1 week to 28/8/2016 : 4/9/2016 The number of days that 31/8/2016 must be adjusted to make it equal to 1/10/2016 : 31 days.