package cn.gov.customs.data.util;

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

/**
 * 时间工具类
 * @author HSY
 *
 */
public class DataUtils {
	
	
	/**
	 *  两个时间差
	 * @Description: 
	 * @author HSY 
	 * @param:
	 */
//	public static long calculationDate(Date start, Date end) {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//		try {
////			Date s = sdf.parse(start);
////			Date e = sdf.parse(end);
//			long diff = start.getTime() - end.getTime();// 这样得到的差值是毫秒级别
//			long days = diff / (1000 * 60 * 60 * 24);
////			long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
////			long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
////			System.out.println("" + days + "天" + hours + "小时" + minutes + "分");
//
//			return diff;
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return 0;
//	}
	/**
	 *  两个时间差
	 * @Description: 
	 * @author HSY 
	 * @param:
	 */
	public static long calculationDate(Date start, Date end) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(sdf.format(start)));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(sdf.format(end)));
		long time2 = cal.getTimeInMillis();
		long between_days=(time2-time1)/(1000*3600*24);
		return between_days;
	} catch (Exception e) {
		e.printStackTrace();
	}
		return 0;
	}
	
	public static  String getDateByNum(Date date, String type, Integer num) {
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		if ("day".equals(type)) { // 天
			cal.add(Calendar.DAY_OF_MONTH, num);
		} else if ("week".equals(type)) {
			cal.add(Calendar.WEEK_OF_YEAR, num);
		} else if ("month".equals(type)) {
			cal.add(Calendar.MONTH, num);
		} else if ("year".equals(type)) {
			cal.add(Calendar.YEAR, num);
		}
		 
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取两个日期之间的日期
	 * @param beginDate
	 * @param endDate
	 * @return yyyy-MM-dd  包含beginDate和endDate
	 * @throws ParseException
	 */
	public static List<String> getDaysBetween(String beginDate, String endDate) throws ParseException {
		List<String> result = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date start = sdf.parse(beginDate);
		Date end = sdf.parse(endDate);
		Calendar tempStart = Calendar.getInstance();
		tempStart.setTime(start);
		Calendar tempEnd = Calendar.getInstance();
		tempEnd.setTime(end);
		tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
		while (tempStart.before(tempEnd)) {
			result.add(sdf.format(tempStart.getTime()));
			tempStart.add(Calendar.DAY_OF_YEAR, 1);
		}
		return result;
	}
	
	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date start = sdf.parse("2019-10-01 00:00:01");
			Date end = sdf.parse("2020-10-09 10:30:00");
			while (calculationDate(start, end) > 8){
				Date date = start;
				String tempEndTime = getDateByNum(date, "day", 10) + " 00:00:01";
				try {
					start = sdf.parse(tempEndTime);
				} catch (ParseException e) {
					e.printStackTrace();
					break;
				}
				System.out.println("========"+ sdf.format(date)+"======" + tempEndTime + "===");
			}
					
			System.out.println("============");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	

}
