0

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?

squarefrog
  • 4,750
  • 4
  • 35
  • 64
Embek
  • 27
  • 1
  • 2
  • 11
  • 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 Answers2

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