UPDATE See the Answer by Meno Hochschild for a much simpler shorter solution.
I leave my Answer here to demonstrate the YearQuarter
class that is likely to be handy in related work.
Using java.time
Much easier with the modern java.time classes rather than the troublesome old date-time classes. Those old classes are now legacy.
This Answer assumes by “quarter” you mean the standard ISO 8601 quarters, the first, second, third, and fourth groups of three months across the calendar year.
The LocalDate
class represents a date-only value without time-of-day and without time zone.
LocalDate start = LocalDate.of ( 2015 , Month.JANUARY , 1 );
LocalDate stop = LocalDate.of ( 2015 , Month.APRIL , 1 );
YearQuarter
Add the ThreeTen-Extra project library to your project to access the YearQuarter
class.
Get start and stop quarters of your dates.
YearQuarter yqStart = YearQuarter.from ( start );
YearQuarter yqStop = YearQuarter.from ( stop );
Loop one quarter at a time until reaching the limit.
The code here bases the loop on the Half-Open approach to defining a span of time, commonly used in date-time work. The beginning is inclusive while the ending is exclusive. I recommend using this approach consistently throughout your logic to avoid ambiguity, confusion, and errors. If instead you insist on the Closed approach (both beginning and ending are inclusive), change the while
test to “while not after”, while( ! yq.isAfter( yqStop ) )
.
int initialCapacity = ( int ) ( ( ChronoUnit.MONTHS.between ( start , stop ) / 3 ) + 3 ); // Probably `+1` is good enough, but I've not thought it through.
List<YearQuarter> yqs = new ArrayList<> ( initialCapacity );
YearQuarter yq = yqStart;
while ( yq.isBefore ( yqStop ) ) { // Using Half-Open approach where the beginning is *inclusive* while the ending is *exclusive*.
yqs.add ( yq ); // Collect this quarter.
// Set up next loop.
yq = yq.plusQuarters ( 1 ); // Move to next quarter.
}
Dump to console.
System.out.println ( "start/stop: " + start + "/" + stop );
System.out.println ( "yqStart/yqStop: " + yqStart + "/" + yqStop );
System.out.println ( "yqs: " + yqs );
start/stop: 2015-01-01/2015-04-01
yqStart/yqStop: 2015-Q1/2015-Q2
yqs: [2015-Q1]
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
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.