package com.seeyon.apps.src_contract.lease.util;

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

public class DateGapUtil {

	/**
	 * 计算两个日期之间相差多少年多少月多少日
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static String getYearMonthDayGap(String startDate,String endDate) throws ParseException {
		int reyear = 0;
		int remonth = 0;
//		设置第一个日期
		String str1 = startDate;
//		  获取第二个日期
		String str2 = endDate;
//		设置日期格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		将字符串转换成日期
		  Date start = sdf.parse(str1);
		  Date end = sdf.parse(str2);
		  Date d3 = null;
		  if(start.getTime()-end.getTime()>0){
			  d3 = start;
			  start = end;
			  end = d3;
		 } 
		  Calendar c1 = Calendar.getInstance();
	      Calendar c2 = Calendar.getInstance();
	      c1.setTime(start);
	      c2.setTime(end);
//	      由于输入的时候结束日期少一天通过代码计算则需要加上此天
	      c2.add(Calendar.DATE, 1);
		  int year1 = c1.get(Calendar.YEAR);
		 int year2 = c2.get(Calendar.YEAR);
		  int month1 = c1.get(Calendar.MONTH);
		  int month2 = c2.get(Calendar.MONTH);
		  int day1 = c1.get(Calendar.DAY_OF_MONTH);
		  int day2 = c2.get(Calendar.DAY_OF_MONTH);
		  int yearInterval = year1 - year2;
		    if (month1 < month2 || month1 == month2 && day1 < day2)
		  yearInterval--;
		  int monthInterval = (month1 + 12) - month2;
		  if (day1 < day2)
		   monthInterval--;
		  monthInterval %= 12;
		  int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
		  int nian = monthsDiff/12;
		  int yue = monthsDiff - (nian*12);
		  c1.add(Calendar.YEAR, nian); 
		  if(day1==day2){
//			  System.out.println(nian+"年");
			  reyear = nian;
//			  System.out.println(yue+"月");
			  remonth = yue;
			  c1.add(Calendar.MONTH, yue);
		  }else {
			  c1.add(Calendar.MONTH, yue-1);
			  if(yue-1<0) {
				  nian--;
				  yue=11;
			  }else {
			  yue =yue-1;
			  }
//			  System.out.println(nian+"年");
			  reyear = nian;
//			  System.out.println(yue+"月");
			  remonth = yue;
//			  c1.add(Calendar.MONTH, yue);
		  }
		  Date dt1 = c1.getTime();
		  long l = dt1.getTime() - c2.getTime().getTime();
		  long tian = l/-(24*60*60*1000);
//		  System.out.println(tian+"天");
//		  System.out.println(sdf.format(dt1));
//		  System.out.println(sdf.format(end));
//		   System.out.println("总共"+monthsDiff+"月");
		  String re = reyear+","+remonth+","+tian;
		   return re;
	}

	
	
	/**
	 * 计算两个日期之间总共相差多少月（多余的天数算作新的一个月）
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException
	 */
	public static int getTotalMonthGap(String startDate,String endDate) throws ParseException {
//		设置第一个日期
		String str1 = startDate;
//		  获取第二个日期
		String str2 = endDate;
//		设置日期格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		将字符串转换成日期
		  Date start = sdf.parse(str1);
		  Date end = sdf.parse(str2);
		  Date d3 = null;
		  if(start.getTime()-end.getTime()>0){
			  d3 = start;
			  start = end;
			  end = d3;
		 } 
		  Calendar c1 = Calendar.getInstance();
	      Calendar c2 = Calendar.getInstance();
	      c1.setTime(start);
	      c2.setTime(end);
//	      由于输入的时候结束日期少一天通过代码计算则需要加上此天
	      c2.add(Calendar.DATE, 1);
		  int year1 = c1.get(Calendar.YEAR);
		 int year2 = c2.get(Calendar.YEAR);
		  int month1 = c1.get(Calendar.MONTH);
		  int month2 = c2.get(Calendar.MONTH);
		  int day1 = c1.get(Calendar.DAY_OF_MONTH);
		  int day2 = c2.get(Calendar.DAY_OF_MONTH);
		  int yearInterval = year1 - year2;
		    if (month1 < month2 || month1 == month2 && day1 < day2)
		  yearInterval--;
		  int monthInterval = (month1 + 12) - month2;
		  if (day1 < day2)
		   monthInterval--;
		  monthInterval %= 12;
		  int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
		  int nian = monthsDiff/12;
		  int yue = monthsDiff - (nian*12);
		  c1.add(Calendar.YEAR, nian); 
		  if(day1==day2){
//			  System.out.println(nian+"年");
//			  System.out.println(yue+"月");
			  c1.add(Calendar.MONTH, yue);
		  }else {
			  c1.add(Calendar.MONTH, yue-1);
			  if(yue-1<0) {
				  nian--;
				  yue=11;
			  }else {
			  yue =yue-1;
			  }
//			  System.out.println(nian+"年");
//			  System.out.println(yue+"月");
//			  c1.add(Calendar.MONTH, yue);
		  }
		  Date dt1 = c1.getTime();
		  long l = dt1.getTime() - end.getTime();
		  long tian = l/-(24*60*60*1000);
//		  System.out.println(tian+"天");
//		  System.out.println(sdf.format(dt1));
//		  System.out.println(sdf.format(end));
//		   System.out.println("总共"+monthsDiff+"月");
		   return monthsDiff;
	}
	
}
