从整数创建 LocalDate 对象

Create LocalDate Object from Integers

如果我已经有一个日期的月、日和年整数,使用它们创建 LocalDate 对象的最佳方法是什么?我找到了这个 post String to LocalDate ,但它以日期的 String 表示开头。

使用 LocalDate#of(int, int, int) 方法获取年月日。

除了 Rohit 的回答之外,您还可以使用此代码从字符串中获取 Localdate

    String str = "2015-03-15";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDate dateTime = LocalDate.parse(str, formatter);
    System.out.println(dateTime);

您可以像这样使用整数创建 LocalDate

      LocalDate inputDate = LocalDate.of(year,month,dayOfMonth);

要从字符串创建 LocalDate,您可以使用

      String date = "04/04/2004";
      inputDate = LocalDate.parse(date,
                      DateTimeFormat.forPattern("dd/MM/yyyy"));

您也可以使用其他格式,但您必须更改 forPattern(...) 中的字符串