java.time
The accepted answer uses java.util date-time API and SimpleDateFormat which was the correct thing to do in 2012. In Mar 2014, the java.util date-time API and their formatting API, SimpleDateFormat were supplanted by the modern date-time API. Since then, it is highly recommended to stop using the legacy date-time API.
Solution using java.time, the modern date-time API:
You do not need a DateTimeFormatter for your date string: java.time API is based on ISO 8601 and therefore you do not need to specify a DateTimeFormatter to parse a date-time string which is already in ISO 8601 format e.g. your date string, 2012-01-01 which can be parsed directly into a LocalDate instance, that contains just date units.
Having parsed the date string into LocalDate, you can add or subtract different date units e.g. years, months, days etc. to it.
Demo:
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
class Main {
public static void main(String args[]) {
LocalDate date = LocalDate.parse("2012-01-01");
System.out.println(date);
LocalDate afterFiveMonths = date.plusMonths(5);
LocalDate beforeFiveMonths = date.minusMonths(5);
System.out.println(afterFiveMonths);
System.out.println(beforeFiveMonths);
// Alternatively,
afterFiveMonths = date.plus(5, ChronoUnit.MONTHS);
beforeFiveMonths = date.minus(5, ChronoUnit.MONTHS);
System.out.println(afterFiveMonths);
System.out.println(beforeFiveMonths);
}
}
Output:
2012-01-01
2012-06-01
2011-08-01
2012-06-01
2011-08-01
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.