package com.jiudian.manage.until;

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

public class TimeUtil {
    /**
     * 获取两个时间段的天数从date2-date1
     * @param date1
     * @param date2
     * @return
     */
    public static int getBetweenDay(String  date1, String date2) {
        Calendar d1 = new GregorianCalendar();
        d1.setTime(formatterTime(date1));
        Calendar d2 = new GregorianCalendar();
        d2.setTime(formatterTime(date2));
        int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
        int y2 = d2.get(Calendar.YEAR);
        if (d1.get(Calendar.YEAR) != y2) {
            do {
                days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
                d1.add(Calendar.YEAR, 1);
            } while (d1.get(Calendar.YEAR) != y2);
        }
        return days;
    }

    public static Date formatterTime(String date){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date newDate=null;
        try {
            newDate= formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newDate;
    }

}
