package com.zcxk.charge.web.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * @author Andy
 * @version V1.0
 * @description: TODO
 * @date 2021/8/17
 **/
public class SceneUtil {

    final static Pattern PATTERN = Pattern.compile("[0-9]*");


    /**
     * 判断是否是整数或者是小数
     * @param str
     * @return true：是，false不是
     */
    public static boolean validateNumber(String str){
        // 判断是否有小数点
        if(str.indexOf(".")>0){
            // 判断是否只有一个小数点
            if(str.indexOf(".")==str.lastIndexOf(".") && str.split("\\.").length==2){
                return PATTERN.matcher(str.replace(".","")).matches();
            }else {
                return false;
            }
        }
        return PATTERN.matcher(str).matches();
    }

    /**
     * 日期是否当前月份
     */
    public static boolean isCurrentYear(String date) throws ParseException {
        LocalDate localDate = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        int y = rightNow.get(Calendar.YEAR);
        if( y== localDate.getYear()){
            return true;
        }
        return false;
    }


    /**
     * 日期增加一月
     */
    public static String subMonth(String date, int n) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, n);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }


    /**
     * 日期是否当前月份
     */
    public static boolean isCurrentMonth(String date) {
        LocalDate localDate = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = null;
        try {
            dt = sdf.parse(date);
        } catch (ParseException e) {
            return false;
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        int y = rightNow.get(Calendar.YEAR);
        int m = rightNow.get(Calendar.MONTH) + 1;
        if( y == localDate.getYear() &&  m == localDate.getMonthValue()){
            return true;
        }
        return false;
    }

    /**
     * 日期增加一年
     */
    public static String subYear(String date, int n) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = null;
        try {
            dt = sdf.parse(date);
        } catch (ParseException e) {
            return null;
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.YEAR, n);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }


}
