package com.smppw.common.rollrange.date.item;

import java.util.Calendar;

/**
 * @author Yu Liu
 *
 */
public class Quarter extends BaseDateTime {

	@Override
	public int between(Calendar startDate, Calendar endDate) {
		int years = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR);
		return years * 4 + (getQuarter(endDate) - getQuarter(startDate));
	}

	@Override
	public Calendar shift(Calendar date, int offside) {
		date.add(Calendar.MONTH, offside * 3);
		return date;
	}

	@Override
	public Calendar startDayOfPeriod(Calendar date) {
		int quarter = getQuarter(date);
		date.set(Calendar.MONTH, (quarter-1) * 3);
		date.set(Calendar.DAY_OF_MONTH, 1);
		return date;
	}
	
	private int getQuarter(Calendar date){
		int month = date.get(Calendar.MONTH); //JANUARY is 0
		return month / 3 + 1;
	}


}
