package com.seeyon.apps.src_ydctleasebill.util;

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

public class DateUtil {

//	查询两个时间相差多少月
	 public int betweenMonthByTwoCalendar(Calendar startCalendar,Calendar endCalendar) {
	    	if(startCalendar.after(endCalendar)){
				Calendar temp = startCalendar;
				startCalendar = endCalendar;
				endCalendar = temp;
			}
//			int startYear = startCalendar.get(Calendar.YEAR);
//			int endYear = endCalendar.get(Calendar.YEAR);
//			int startMonth = startCalendar.get(Calendar.MONTH);
//			int endMonth = endCalendar.get(Calendar.MONTH);
//			int monthNum = (endYear - startYear)*12+(endMonth-startMonth);
//	    	return monthNum;

		 int yearsDiff = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
		 int monthsDiff = yearsDiff * 12 + endCalendar.get(Calendar.MONTH)- startCalendar.get(Calendar.MONTH);
		 if (endCalendar.get(Calendar.DAY_OF_MONTH) > startCalendar.get(Calendar.DAY_OF_MONTH)) {
			 monthsDiff++;
		 }
		 return monthsDiff;


	    }
	 
	 public String discrepancyDay(String str1,String str2) throws ParseException {
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//			将字符串转换成日期
			  Date start = sdf.parse(str1);
			  Date end = sdf.parse(str2);
//			  Date end = new Date();
			  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(c2.DATE,1);
		      end = c2.getTime();
//		      System.out.println(sdf.format( c2.getTime()));
			  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){
				  c1.add(Calendar.MONTH, yue);
			  }else {
				  
				  c1.add(Calendar.MONTH, yue-1);
				  if(yue-1<0) {
					  nian--;
					  yue=11;
				  }else {
				  yue -=1;
				  }
			  }
//			  System.out.println(nian+"年");
//			  System.out.println(yue+"月");
			  Date dt1 = c1.getTime();
			  long l = dt1.getTime() - end.getTime();
			  long tian = l/-(24*60*60*1000);
			  return nian+"-"+yue+"-"+tian;
	 }


	public int getCycleNum (String cycle){
		int cycleNum = 1;
		if("月".equals(cycle)){
			cycleNum = 1;
		}else if("季".equals(cycle)){
			cycleNum = 3;
		}else if("半年".equals(cycle)){
			cycleNum = 6;
		}else if("年".equals(cycle)){
			cycleNum = 12;
		}else if("一次性".equals(cycle)){
			cycleNum = 99;
		}
		return cycleNum;
	}

	public double stringToNum(String str){
		double i = 0 ;
		String[] strs = str.split(",");
		StringBuilder numString = new StringBuilder();
		for (String value: strs) {
			numString.append(value);
		}
		i = Double.parseDouble(numString.toString());
		return i;
	}

}
