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 653 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
- 168,305
- 31
- 280
- 331
3
If you can use Java 8
, use LocalDate.plusMonths()
LocalDate localDate = LocalDate.now();
localDate.plusMonths(numberOfMonths);

Jordi Castilla
- 26,609
- 8
- 70
- 109