package com.dev.swms.server.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


/**
 *@author  dev
 *@version $Project:ecad $Date: 2010/06/01 09:14:16 $Version:V1.1
 *日期之间的间隔
 */
public class DateUtils {

	/**
	 * 两个日期之间的间隔天数
	 * @param sDate
	 * @param eDate
	 * @return
	 */
	public static Integer getBetweenDays(Date sDate,Date eDate) {
		long DAY = 24L * 60L * 60L * 1000L; 
		return new Integer((int)((eDate.getTime() - sDate.getTime())/DAY));
	}
	/**
	 * 两个日期之间的间隔月数
	 * @param sDate
	 * @param eDate
	 * @return
	 */
	public static Integer getBetweenMonths(Date sDate,Date eDate) {
		int betweentYears = getBetweenYears(sDate,eDate);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sDate);
		int month1 = calendar.get(Calendar.MONTH);
		calendar.setTime(eDate);
		int month2 = calendar.get(Calendar.MONTH);
		return new Integer(betweentYears*12 +( month2 - month1));
	}
	/**
	 * 两个日期之间间隔的年数
	 * @param sDate
	 * @param eDate
	 * @return
	 */
	public static Integer getBetweenYears(Date sDate,Date eDate) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sDate);
		int year1 = calendar.get(Calendar.YEAR);
		calendar.setTime(eDate);
		int year2 = calendar.get(Calendar.YEAR);
		return new Integer(year2 - year1);
	}
	/**
	 * 目标日期是否在源日期之前
	 * @param sDate
	 * @param eDate
	 * @return
	 */
	public static Boolean before(Date sDate , Date eDate) {
		return eDate.before(sDate);
	}
	/**
	 * 目标日期是否在源日期之后
	 * @param sDate
	 * @param eDate
	 * @return
	 */
	public static Boolean after(Date sDate , Date eDate) {
		return eDate.after(sDate);
	}
	public static void main(String[] args) throws ParseException {
		SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd");
		Date sDate = fomat.parse("2008-01-01");
		Date eDate = fomat.parse("2008-03-01");
		
		System.out.println( "相差日："+ getBetweenDays(sDate,eDate));
		System.out.println( "相差月："+ getBetweenMonths(sDate,eDate));
		System.out.println( "相差年："+ getBetweenYears(sDate,eDate));
	}
	
//	得到baseDate这天所在周的第一天
	public static Date getFirstDateOfWeek(Date baseDate){
		Calendar cal = new GregorianCalendar();
		cal.setTime(baseDate);
		int dayofweek = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayofweek == 0)
		   dayofweek = 7;
		cal.add(Calendar.DATE, -dayofweek + 1);
		return cal.getTime();
	}
//	得到baseDate这天所在周的最后一天
	public static Date getLastDateOfWeek(Date baseDate){
		Calendar cal = new GregorianCalendar();
		cal.setTime(getFirstDateOfWeek(baseDate));
		cal.add(Calendar.DATE,6);
		return cal.getTime();
	}
//	得到baseDate这天所在月的第一天
	public static Date getFirstDateOfMonth(Date baseDate){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = new GregorianCalendar();
		cal.setTime(baseDate);
		String fdOfMonth = cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)+1)+"-01";//月的第一天
		try {
			return sdf.parse(fdOfMonth);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
//	得到baseDate这天所在月的第一天
	public static Date getLastDateOfMonth(Date baseDate){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = new GregorianCalendar();
		cal.setTime(baseDate);
		cal.add(Calendar.MONTH,1);
		cal.setTime(getFirstDateOfMonth(cal.getTime()));
		cal.add(Calendar.DATE,-1);
		return cal.getTime();
	}
//	得到baseDate这天的第二天
	public static Date getNextDay(Date baseDate){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = new GregorianCalendar();
		cal.setTime(baseDate);
		cal.add(Calendar.DATE,1);
		return cal.getTime();
	}
	
}
