Java - What is the output: add one month to 2014-01-31

Question

What is the output from the following code

LocalDate ld1 = LocalDate.of(2014, Month.JANUARY, 31);
LocalDate ld2 = ld1.plusMonths(1);
System.out.println(ld1);
System.out.println(ld2);


Click to view the answer

2014-02-28

Note

After adding the month, the result is checked if it is a valid date.

If it is not a valid date, the day of month is adjusted to the last day of the month.

In this case, the result would be 2014-02-28.

Demo

import java.time.LocalDate;
import java.time.Month;

public class Main {
  public static void main(String[] args) {
    LocalDate ld1 = LocalDate.of(2014, Month.JANUARY, 31);
    LocalDate ld2 = ld1.plusMonths(1);
    System.out.println(ld1);//  www .j  av a 2  s  . co  m
    System.out.println(ld2);
  }
}

Result

Related Quiz