23
votes

If I already have a date's month, day, and year as integers, what's the best way to use them to create a LocalDate object? I found this post String to LocalDate , but it starts with a String representation of the date.

3

3 Answers

37
votes

Use LocalDate#of(int, int, int) method that takes year, month and dayOfMonth.

18
votes

You can create LocalDate like this, using ints

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

and to create LocalDate from String you can use

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

You can use other formats too but you have to change String in forPattern(...)

6
votes

In addition to Rohit's answer you can use this code to get Localdate from String

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