-4

我可以用什么方式格式化拍卖的本地日期,不断收到错误消息,要求将构造函数更改为字符串,但我需要它是 LocalDate

公共静态无效主要(字符串[]参数){

    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");
4

2 回答 2

0

执行此操作的一种方法是按照错误说明进行操作,并将构造函数更改为字符串:

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
  }
}
于 2017-03-23T12:49:08.357 回答
0

据我了解您的评论,您希望您的构造函数接受LoaclDate. 像这样:

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

这是一个使用 type 参数声明构造函数的问题LocalDate。例如:

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

}

我刚刚输入了一个日期格式,它几乎不是你想要的,如果你想要格式化并且不想只使用LocalDate.toString(). 我可能还缺少其他东西,因为老实说你没有很好地解释。

于 2017-03-24T06:43:26.043 回答