I have a constructor that takes a String and turns that String into a date. However when I run my main code, I get random dates that are not correct. I made a test class just to test the code in my constructor and everything runs fine, I get the appropriate output. However, my getter method is giving me the wrong date. Also, the if statements are not checking the validity of my parameters either. Not sure how to fix it.
import java.util.Calendar;
import java.util.Date;
import java.lang.Integer;
public class RealEstateSale{
private String country;
private double price;
private Date dateOfSale;
CurrencyConverter cc = new CurrencyConverter();
public String getCountry(){
return country;
}
public RealEstateSale(double price){
this.price = price;
if(price < 0){
country = null;
}
}
public double getPrice(){
return price;
}
public RealEstateSale(String country, String date){
this.country = country;
String [] tokens = date.split("/");
int month = Integer.parseInt(tokens[0]);
int day = Integer.parseInt(tokens[1]);
int year = Integer.parseInt(tokens[2]);
Calendar dateMaker = Calendar.getInstance();
dateMaker.set(year, month-1, day);
dateOfSale = dateMaker.getTime();
if(0 > year || 0 > month || 0 > day){
this.country = null;
} else if (month > 11){
this.country = null;
} else if(day > 31){
this.country = null;
} else if (!cc.countryCodes.contains(country)){
this.country = null;
}
}
public Date getDate(){
return dateOfSale;
}
}
So lets say I put in the date 1/10/1997, I'd get the date 4/20/0007. I don know where this date is coming from.