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

import java.util.Calendar;

/**
 * @author Yu Liu
 *
 */
public class HalfYear extends BaseDateTime {
	
	@Override
	public int between(Calendar startDate, Calendar endDate) {
		int years = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR);
		return years * 2 + (getHalfYear(endDate) - getHalfYear(startDate));
	}

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

	@Override
	public Calendar startDayOfPeriod(Calendar date) {
		int halfYear = getHalfYear(date);
		date.set(Calendar.MONTH, (halfYear - 1) * 6);
		date.set(Calendar.DAY_OF_MONTH, 1);
		return date;
	}

	private int getHalfYear(Calendar date) {
		int month = date.get(Calendar.MONTH); // JANUARY is 0
		return month / 6 + 1;
	}

}
