-4

What way can I format the localdate for the auctions, Keep getting an error asking to change constructor to a string but I need it to be a LocalDate

public static void main(String[] args) {

    user1 = new User("fred", "fredbloggs");
    user2 = new User("joe", "joebloggs");
    auction1 = new Auction(1000.00, 5000.00, 2017-04-05);
    auction2 = new Auction(30.00, 80.00,  );
    item1 = new Item("Car - Honda Civic VTI 1.8 Petrol");
    item2 = new Item("Sony Bluetooth Speakers");
  • [DateFormatter](https://docs.oracle.com/javase/7/docs/api/javax/swing/text/DateFormatter.html) is your friend – Jens Mar 23 '17 at 12:31
  • If you are not using Swing, I would suggest [`DateTimeFormatter`](http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) – Ole V.V. Mar 23 '17 at 12:41
  • do I need to use that in the constructor in the my auction class? – Cormac Valentine Mar 23 '17 at 12:42
  • I don’t see any constructor nor any `LocalDate` in your code, so I cannot tell. – Ole V.V. Mar 23 '17 at 12:42
  • It sounds like a design decision. Not knowing the rest of your design, my first thought is have the `Auction` constructor accept a `LocalDate` object and have the class handle the formatting only when a formatted date is needed. – Ole V.V. Mar 23 '17 at 12:44
  • That's my problem I cant get the auction constructor to accept LocalDate – Cormac Valentine Mar 23 '17 at 12:48

2 Answers2

0

One way of doing this would be to follow as the error says, and change your constructor to a string:

auction1 = new Auction(1000.00, 5000.00, "2017-04-05");
//rest of code


//then change the class definition 
public class Auction{
  LocalDate auctionDate;
  //declare other members here

  public Auction(int startingPrice,int buyoutPrice,String auctionDate){
  this.auctionDate=auctionDate;
  //set up other members accordingly
  }
}
  • Unfortunately this gives me many errors in the rest of my project. Is there any way to solve the problem while keeping the constructor a Local date? – Cormac Valentine Mar 23 '17 at 13:05
0

As I understand your comment you want your constructor to accept a LoaclDate. Like this:

    Auction auction1 = new Auction(1000.00, 5000.00, LocalDate.of(2017, 4, 5));

That’s a question of declaring the constructor with a parameter of type LocalDate. For example:

public class Auction {

    private static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("EEEE d/L uuuu");

    // other instance fields
    LocalDate auctionDate;

    public Auction(double startingPrice, double buyoutPrice, LocalDate auctionDate) {
        // set other instance fields
        this.auctionDate = auctionDate;
    }

    // methods

    public String getFormattedAuctionDate() {
        return auctionDate.format(dateFormatter);
    }

}

I have just put in a date format, it’s hardly the one you want, please provide your own if you want formatting and don’t want to just use LocalDate.toString(). There are probably other things I am missing since honestly you didn’t explain very well.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161