Click here to Skip to main content
15,891,833 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
//I am having troubles trying to figure out how to compare two dates in my class. In my DateClient, I created two dates, but don't know how to use them. If you're wondering I'm creating a date class to compare the dates to find how //many days ago was that date and on what day was it.
//Date d1 = new Date(2015,10,20);
//Date d2 = new Date(2000,3,12);

public class Date {

private int year;
private int day;
private int month;

public Date(int year, int month, int day){
// this.year = year;
// this.month = month;
// this.day = day;
year=0;
month=0;
day=0;
}
public int getYear(){
return year;
}
public void setYear(){
this.year=year;
}
public int getMonth(){
return month;
}
public void setMonth(){
this.month=month;
}
public int getDay(){
return day;
}
public void setDay(){
this.day=day;
}
public String toString(){
return (year+"/"+month+"/"+day);
}
public boolean equals(Date D){
boolean Same = false;
if (day==this.day && month==this.month && year==this.year){
Same = true;
} else Same = false;
return Same;
}
public boolean isLeapYear(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
return true;
} else return false;

}
public void nextDay(){
day++;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 && day == 32){
month++;
day = 1;
} else if (month == 4 || month == 6 || month == 9 || month == 11 && day == 31){
month++;
day = 1;
} else if (month == 12 && day == 32){
month = 1;
year++;
day = 1;
} else if (month == 2){
if (isLeapYear(year) == true && day == 30){
day = 1;
} else if (isLeapYear(year) == false && day == 29){
day = 1;

}
}
}
public int advanceTo(Date endDay){
int countDays = 0;
while ( != endDay){
countDay++;
}
}

}


}
<pre lang="java"><pre lang="java"><pre lang="java"><pre lang="java">
Posted

You can use Calender.before(), Calender.after() and Calender.equals() like this way:
Java
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);

if(cal1.after(cal2)){
    System.out.println("Date1 is after Date2");
}
if(cal1.before(cal2)){
    System.out.println("Date1 is before Date2");
}
if(cal1.equals(cal2)){
     System.out.println("Date1 is equal Date2");
}
 
Share this answer
 
v2
Handling dates is not an easy task.

First of all, I remeber you that that Java has its classes to handle dates,
in the package java.times (perhaps too many classes ...)
If you really want to handle them "by hand" ...


Usually is better to maintain an internal representation of the date as e.g., the number of days since a given date (e.g. 1/1/1900) or the number of milliseconds if you are interested also on times.

let's call this variable "intdate"

private int intdate;

And let's export this value to be used from other objects.

public int getIntdate (){
return intdate;
}

After that you should have a constructor that creates a new date based
on year, month and day :

public mydate (int year, int month, int day) {
this.intdate = ... // I leave this to you !!
}

And another constructor that accept the internal representation :
public mydate (int adate) {
this.intdate = adate;
}


Having done that, the functions nextday and prevday should return a new
object (better not to modify the current one)


public mydate nextday ()
return new mydate (this intdate+1);
}

public mydate prevday () {
return new mydate (this.intdate-1);
}


You can obtain a date of N days in the future or in the past by simply adding
a number to the current value, and returning a new date


// calculate a date in the future or in the past by adding a positive or negative number of days
public mydate addDays (int days) {
return new mydate (this.intdate + days);
}


The function advance to date is useless. what's its purpose ?


You can calculate the number of days between two dates by simply evaluating the difference of the internal representations.


// returns the number of days between otherdate and this instance
// positive if otherdate is greater than this
public int daysTo (mydate otherdate){
return otherdate.getIntdate() - this.intdate;
}

you can compare dates just testing the internal values

// return true if this date is equal to the other date
public boolean isEqual (mydate otherdate){
return this.intdate == otherdate.getIntdate();
}

// return true if this date is less then the other date
public boolean isLessThan (mydate otherdate){
return this.intdate < otherdate.getIntdate();
}

// return true if this date is greater then the other date
public boolean isGreaterThan (mydate otherdate){
return this.intdate > otherdate.getIntdate();
}


All I leave to you is to build the internal value given day, month, year
and to obtain back day, month or year from the internal value

public int day {
extract and return the day from the intdate ...
}

public int month {
extract and return the month from the intdate ...
}

public int year {
extract and return the year from the intdate ...
}

so your initial question is solved in this way :
mydate d1 = new mydate (2015,10,20);
mydate d2 = new mydate (2000,3,12);

int difference = d2.daysTo(d1);
 
Share this answer
 
Comments
Member 12203056 12-Dec-15 22:09pm    
Wow thanks so much for your hard work.. it's my turn to do the same..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900