LocalDate格式

时间:2017-03-23 12:28:09

标签: java localdate

我可以用什么方式格式化拍卖的本地日期,继续收到错误,要求将构造函数更改为字符串,但我需要将其作为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");

2 个答案:

答案 0 :(得分: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
  }
}

答案 1 :(得分:0)

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

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

这是一个用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(),请提供您自己的日期格式。可能还有其他我不想要的事情,因为老实说你没有解释得很好。