Java end of month plusMonths

What happens to plus Months towards the end of the month?

There is no 31st in February, but I was curious about what would happen if I added 1 month to 1/31 with plusMonths ().

According to Documentation

This method adds the specified amount to the month field in three steps.

  1. Add the number of months entered to the month field
  2. Check if the resulting date is invalid
  3. If necessary, adjust the "Month Day" to the last valid day For example, adding January to 2007-03-31 results in an invalid date 2007-04-31. Instead of returning an invalid result, 2007-04-30, the last valid day of the month, is selected.

First, add the months and check if there is a date corresponding to the added month. If there is no such date, it seems to return the last day of the month.

Verification

LocalDate date = LocalDate.of(2020,1,31);
System.out.println(date.plusMonths(1));
// 2020-02-29

LocalDate date = LocalDate.of(2020,1,30);
System.out.println(date.plusMonths(1));
// 2020-02-29

LocalDate date = LocalDate.of(2020,1,29);
System.out.println(date.plusMonths(1));
// 2020-02-29

LocalDate date = LocalDate.of(2020,1,28);
System.out.println(date.plusMonths(1));
// 2020-02-28

For dates that don't apply, you'll get a valid date back: clap:

Digression

What if minusMonths is followed by plusMonths?

LocalDate date = LocalDate.of(2020,1,31);
System.out.println(date.plusMonths(1).minusMonths(1));
// 2020-01-29

The result was 2020-01-29 because I did minusMonths on 2020-02-29 and it became 2020-02-29 with plusMonths.

Recommended Posts

Java end of month plusMonths
[Java] Overview of Java
Predicted Features of Java
[Java] Significance of serialVersionUID
Review of java Shilber
java --Unification of comments
History of Java annotation
java (merits of polymorphism)
NIO review of java
[Java] Three features of Java
Summary of Java support 2018
[Java] Mirage-Basic usage of SQL
[Java] Beginner's understanding of Servlet-②
[Java] Practice of exception handling [Exception]
Basics of character operation (java)
[Java] Creation of original annotation
4th day of java learning
[Java] Beginner's understanding of Servlet-①
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
[Java] Implementation of Faistel Network
[Java] Comparator of Collection class
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
Enumeration of all combinations Java
java (inheritance of is-a principle)
Implementation of gzip in java
Advantages and disadvantages of Java
How to derive the last day of the month in Java
Benefits of Java static method
[Java] Delete the specified number of characters from the end of StringBuilder
[Java] Summary of control syntax
Implementation of tri-tree in Java
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
Method to add the number of years and get the end of the month
[Java] Speed comparison of string concatenation
Think of a Java update strategy
[Java] Delete the elements of List
[For beginners] Summary of java constructor
Various methods of Java String class
Root cause of java framework bugs
About fastqc of Biocontainers and Java
[Java version] The story of serialization
Summary of [Java silver study] package
About Lambda, Stream, LocalDate of Java8
Story of passing Java Gold SE8
Sort a List of Java objects
[Java] Output by FormatStyle of DateTimeFormatter
[Java] Input to stdin of Process
Basic usage of java Optional Part 1
Handling of time zones using Java
[Java EE] Common misunderstanding of @Dependent
[Note] Handling of Java decimal point
[Java] Runtime Data Areas of JVM
List of members added in Java 9
[Java] Judgment of identity and equivalence
Consideration of "Java policy change" risk
[Java] Reference / update of Active Directory