Current Quarter w/Guava

Similar to Getting a current quarter in Java this example takes it a step further. It uses Guava RangeMap and contains three methods getQuarter, getQuarterBeginDate and getQuarterEndDate.

Setup

private RangeMap<Date, Integer> quarters = TreeRangeMap.create();
private Date dateToCompare;

Class

public CurrentQuarterGuava (Date date) {
    
    checkNotNull(date);
    
    this.dateToCompare = date;
    
    // create a JODA current time instance
    DateTime currentDate = new DateTime(dateToCompare);

    // set quarters
    DateTime q1BeginDate = new DateTime(currentDate.getYear(), 1, 1, 0, 0, 0, 0);
    DateTime q1EndDate = new DateTime(currentDate.getYear(), 3, 31, 0, 0, 0, 0);

    DateTime q2BeginDate = new DateTime(currentDate.getYear(), 4, 1, 0, 0, 0, 0);
    DateTime q2EndDate = new DateTime(currentDate.getYear(), 6, 30, 0, 0, 0, 0);

    DateTime q3BeginDate = new DateTime(currentDate.getYear(), 7, 1, 0, 0, 0, 0);
    DateTime q3EndDate = new DateTime(currentDate.getYear(), 9, 30, 0, 0, 0, 0);

    DateTime q4BeginDate = new DateTime(currentDate.getYear(), 10, 1, 0, 0, 0, 0);
    DateTime q4EndDate = new DateTime(currentDate.getYear(), 12, 31, 0, 0, 0, 0);

    //add to quarters
    quarters.put(Range.closed(q1BeginDate.toDate(), q1EndDate.toDate()), 1);
    quarters.put(Range.closed(q2BeginDate.toDate(), q2EndDate.toDate()), 2);
    quarters.put(Range.closed(q3BeginDate.toDate(), q3EndDate.toDate()), 3);
    quarters.put(Range.closed(q4BeginDate.toDate(), q4EndDate.toDate()), 4);
}

Get Quarter

/**
 * Method should return a integer representing
 * the quarter in which the quarter date is in. 
 * 
 * @return integer 
 */
public Integer getQuarter () {
    return quarters.get(dateToCompare);
}

Get Quarter Begin Date

/**
 * Method should return the quarter begin date
 * 
 * @return Date
 */
public Date getQuarterBeginDate () {
    
    Entry<Range<Date>, Integer> quarterAsDate = quarters.getEntry(dateToCompare);
    
    Range<Date> range = quarterAsDate.getKey();
    
    return range.lowerEndpoint();
}

Get Quarter End Date

/**
 * Method should return the quarter end date
 * 
 * @return Date
 */
public Date getQuarterEndDate () {
    
    Entry<Range<Date>, Integer> quarterAsDate = quarters.getEntry(dateToCompare);

    Range<Date> range = quarterAsDate.getKey();
    
    return range.upperEndpoint();
}

Unit tests

Current quarter test

@Test
public void current_quarter () {
    
    DateTime firstQuarterCheck = new DateTime(2013, 1, 15, 0, 0, 0, 0);
    
    CurrentQuarterGuava currentQuarterWithGuava = 
            new CurrentQuarterGuava(firstQuarterCheck.toDate());
    
    assertEquals(new Integer(1), 
            currentQuarterWithGuava.getQuarter());
}

Current quarter begin test

@Test
public void current_quarter_begin_date () {

    DateTime firstQuarterCheck = new DateTime(2013, 1, 15, 0, 0, 0, 0);

    CurrentQuarterGuava currentQuarterWithGuava = 
            new CurrentQuarterGuava(firstQuarterCheck.toDate());
    
    assertEquals(quarterBeginDate.toDate(), 
            currentQuarterWithGuava.getQuarterBeginDate());

}

Current quarter end test

@Test
public void current_quarter_end_date () {

    DateTime firstQuarterCheck = new DateTime(2013, 1, 15, 0, 0, 0, 0);

    CurrentQuarterGuava currentQuarterWithGuava = 
            new CurrentQuarterGuava(firstQuarterCheck.toDate());
    
    assertEquals(quarterEndDate.toDate(), 
            currentQuarterWithGuava.getQuarterEndDate());

}