I have searched a lot on internet about how to get current time in Java but what I am looking for is how to get current time + n months?
Asked
Active
Viewed 583 times
0
-
Be careful though, be sure to check your use case. Adding a month, what does this mean? What should happen 31st of January when you add a month? Should you get 28th of Feb (and in some years 29th)? (probably yes). What is your use case? – Roy van Rijn Dec 16 '15 at 11:52
2 Answers
5
You can try like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, numberofMonths);
Rahul Tripathi
- 161,154
- 30
- 262
- 319
3
If you can use Java 8, use LocalDate.plusMonths()
LocalDate localDate = LocalDate.now();
localDate.plusMonths(numberOfMonths);
Jordi Castilla
- 25,851
- 7
- 65
- 105