0

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.

David
  • 1
  • 1
  • Do you need to call `dateMaker.computeTime` before trying to get the time? – Jonathon K Apr 28 '19 at 20:19
  • I don't think so. In my test code I used the same exact code and I got the correct output. Im sure the code Im using for the date is correct, I just suspect it has something to do with the way the constructor is getting and setting the date – David Apr 28 '19 at 20:59
  • 1
    I can't reproduce the issue with the code you posted: https://ideone.com/e8xzhB. Could you maybe add a reproducible runnable example? – TiiJ7 Apr 29 '19 at 06:32

1 Answers1

2

If your Java version supports it I would recommend using LocalDate instead of Calendar/Date

private LocalDate dateOfSale;
...

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]);

    dateOfSale = LocalDate.of(year, month, day); 
    ...
}

If you need to have dateOfSale declared as Date you can still skip Calendar

LocalDate localDate = LocalDate.of(year, month, day); 
dateOfSale = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); 

Conversion from this answer

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Good improvement over the code in the question. You may also use a `DateTimeFormatter` and `LocalDate.parse()` for parsing the date string rather than `date.split()`. – Ole V.V. Apr 29 '19 at 11:27
  • This still doesnt really answer my question. I asked why Im getting bad dates from my constructors. My date code works just fine and when I use your revised code I still get the same answer. My issue isn't with the dates, I want to know where I went wrong with my constructors. – David Apr 29 '19 at 15:17
  • @David Then the error isn’t in your constructor – Joakim Danielson Apr 29 '19 at 15:51