package com.microservices.dss.utils;

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

public class DateUtil {

    public static final String PATTERN_DATE_ONLY_YEAR_MONTH = "yyyy-MM";

    /**
     * @author wanjia
     * Description 日期运算，加减年份
     * Date 2021/5/12 19:47
     * @param date 传入日期 yyyy-mm-dd
     * @param i 被加数
     * @return java.lang.String
     **/
    public static String DateAddByYear(String date,int i) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM");
        Date date1 = df.parse(date);
        calendar.setTime(date1);
        calendar.add(Calendar.YEAR,i);
        return df.format(calendar.getTime());
    }

    public static String OnlyYearMonthDateAddByMonth(String date,int i) throws ParseException {
        Calendar calendar;
        calendar = Calendar.getInstance();
        SimpleDateFormat df=new SimpleDateFormat(PATTERN_DATE_ONLY_YEAR_MONTH);
        Date date1 = df.parse(date);
        calendar.setTime(date1);
        calendar.add(Calendar.MONTH,i);
        return df.format(calendar.getTime());
    }

    public static String currentYearMonth() {
        return new SimpleDateFormat("yyyy-MM").format(Calendar.getInstance().getTime());
    }

    public static String getFormattedCurrentDate() {
        return new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
    }

    /**
     * 得到当前日期的结束时间
     */
    public static String getFormattedCurrentDateTime() {
        return getFormattedCurrentDate() + " 23:59:59";
    }

    /**
     * 得到当前时间前一年同月的第一天的日期
     **/
    public static String getFormattedPreviousYearSameMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        return new SimpleDateFormat("yyyy-MM-01 00:00:00").format(calendar.getTime());
    }
}
