The other Answers are correct but outdated.
Strings != Date-Times
I want to result format to be yyyy-mm-dd NOT the Java date format which is Day Month dd time time-zone yyyy
You are confusing a date-time value with a String that represents that value. So there is no such thing as a “Java date format”. You can generate a String in any format you desire, or generate one automatically in a localized format.
The input string you desire is a standard ISO 8601 format. The java.time classes use these standard formats by default.
java.time
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
. The Joda-Time team also advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate start = LocalDate.parse( "1986-09-20" );
This class can do math, adding years and weeks as you desire.
Apparently you want to start with the first occurrence of the day-of-week of your input date in the following month. The TemporalAdjuster
interface and TemporalAdjusters
class (note the plural) provide such assistance. In particular, TemporalAdjusters.nextOrSame( DayOfWeek )
where DayOfWeek
is a handy enum for the seven days of the week.
DayOfWeek dow = start.getDayOfWeek();
LocalDate monthLater = start.plusMonths( 1 ); // If day 29-31 not found in following month, the last day of month is used instead. So always next month, not two months out.
LocalDate firstOfNextMonth = monthLater.withDayOfMonth( 1 );
TemporalAdjuster adjuster = TemporalAdjusters.nextOrSame( dow );
LocalDate firstDayOfWeekOfNextMonth = firstOfNextMonth.with( adjuster );
And a year later.
LocalDate yearLaterFromFirstDayOfWeekOfNextMonth = firstDayOfWeekOfNextMonth.plusYears( 1 );
Strings
To generate a String in your desired ISO 8601 standard format, simply call toString()
.
String output = yearLaterFromFirstDayOfWeekOfNextMonth.toString();
Looping
You can loop date-time values by testing with equals
,compareTo
, isEqual
, isBefore
, and isAfter
methods found on the various java.time classes.
LocalDate ld = start;
while ( ! ld.isAfter( firstDayOfWeekOfNextMonth ) ) {
String output = ld.toString();
…
// Prepare for next loop.
ld = ld.plusWeeks( 1 );
}
I suggest determining your boundary dates first, to keep the business logic clear. Then afterwards do the looping week-by-week as that is incidental to the logic. Mixing the two issues (boundary dates & week-by-week) is what makes your Question confusing.