whats wrong? help to newbie
hi all,
can any one tell me what's wrong?
i'm triyng to get next date from object that I created and still getting the same date
Code :
public Date nextDay()
{ int newDay=this.day, newMonth=this.month, newYear=this.year;
if ((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) || (this.month == 8) || (this.month == 10)){
if (this.day < 31)
newDay = +this.day;
if (this.day == 31)
newDay = 01;
newMonth = +this.month;
}
if ((this.month == 4) || (this.month == 6) || (this.month == 9) || (this.month == 11)){
if (this.day < 30)
newDay = +this.day;
if (this.day == 30)
newDay = 01;
newMonth = +this.month;
}
if (this.month == 2){
if (this.day < 28)
newDay = +this.day;
if (this.day == 28)
newDay = 01;
newMonth = +this.month;
if (((this.year%4 == 0) && (this.year%100!=0) || (this.year%400==0)) && (this.day==28))
newDay = +this.day;
}
if (this.month == 12){
if (this.day < 31)
newDay = +this.day;
if (this.day == 31)
newYear = +this.year;
newMonth = 01;
newDay = 01;
}
return new Date(newDay, newMonth, newYear);
}
Re: whats wrong? help to newbie
Quote:
still getting the same date
Please explain.
Add some printlns to your program to show the problem.
Print out the values of all the variables that you are using to construct the Date object.
How can it be the same date? You create a new instance of the Date class.
Re: whats wrong? help to newbie
Re: whats wrong? help to newbie
here is the whole code:
Code :
public class Date
{
private int day;
private int month;
private int year;
public Date( int day, int month, int year)
{
this.day=day;
this.month=month;
this.year=year;
}
public Date(Date d )
{
this.day=d.day;
this.month=d.month;
this.year=d.year;
}
public int getDay()
{
return this.day;
}
public int getMonth()
{
return this.month;
}
public int getYear()
{
return this.year;
}
public void setDay(int day)
{
this.day=day;
}
public void setMonth(int month)
{
this.month=month;
}
void setYear(int year)
{
this.year=year;
}
public String toString()
{
String stDay=""+this.day;
if (this.day<10)
stDay="0"+this.day;
String stMon=""+this.month;
if (this.month<10)
stMon="0"+this.month;
return ("date: "+stDay+"/"+stMon+"/"+this.year);
}
public Date nextDay()
{ int newDay=this.day, newMonth=this.month, newYear=this.year;
if ((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) || (this.month == 8) || (this.month == 10)){
if (newDay < 31)
newDay = +this.day;
else
newDay = 01;
newMonth = +this.month;
}
if ((this.month == 4) || (this.month == 6) || (this.month == 9) || (this.month == 11)){
if (this.day < 30)
newDay = +this.day;
if (this.day == 30)
newDay = 01;
newMonth = +this.month;
}
if (this.month == 2){
if (this.day < 28)
newDay = +this.day;
if (this.day == 28)
newDay = 01;
newMonth = +this.month;
if (((this.year%4 == 0) && (this.year%100!=0) || (this.year%400==0)) && (this.day==28))
newDay = +this.day;
}
if (this.month == 12){
if (this.day < 31)
newDay = +this.day;
if (this.day == 31)
newYear = +this.year;
newMonth = 01;
newDay = 01;
}
return new Date(newDay, newMonth, newYear);
}
I'm using bluej so I can create an object with parameters and trying to do nextDay, getting the same date that I set in object before
Re: whats wrong? help to newbie
Quote:
Originally Posted by
Tjstretch
thanks, now when I'm changed the code to
I'm getting +2
tried with date 3 1 85 and got 5 3 85
Re: whats wrong? help to newbie
Add some printlns to your program to show the problem.
Print out the values of all the variables that you are using to show their values before and after.
Add some comments describing what is wrong with the printed output.
Also the java SE has a class named: Date.
It would be less confusing to anyone reading your code if you chose a unique name for your class.
Re: whats wrong? help to newbie
ok, so I found the problem now it's work
here is the code:
Code :
{ int newDay=this.day, newMonth=this.month, newYear=this.year;
if ((this.month == 1) || (this.month == 3) || (this.month == 5) || (this.month == 7) || (this.month == 8) || (this.month == 10)){
if (this.day < 31){
newDay = ++this.day;
}
if (this.day == 31){
newDay = 01;
newMonth = ++this.month;
}
}