huanghongfa
2021-06-09 c0a7fdfcf9f74359fbecf2addc570e13be374dbc
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DateUtils.java
@@ -2,6 +2,7 @@
import lombok.extern.slf4j.Slf4j;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
@@ -594,14 +595,59 @@
        }
    }
    /**
     * 日期相加减
     * @param time
     *             时间字符串 yyyy-MM-dd HH:mm:ss
     * @param num
     *             加的数,-num就是减去
     * @return
     *             减去相应的数量的年的日期
     * @throws ParseException
     */
    public static Date yearAddNum(Date time, Integer num) {
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //Date date = format.parse(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.YEAR, num);
        Date newTime = calendar.getTime();
        return newTime;
    }
    public static boolean isValidDate(String str) {
        boolean convertSuccess=true;
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess=false;
        }
        return convertSuccess;
    }
    public static Date toValidDate(String str) {
        Date date = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        try {
            format.setLenient(false);
            date = format.parse(str);
        } catch (ParseException e) {
            log.error("党员导入日期格式错误");
            date = new Date();
        }
        return date;
    }
    public static void main(String[]args)throws Exception{
//        Date date= new Date();
//        Date after = new Date();
//        System.out.println(calTimeDifference(date,after));
        isValidDate("2020/12/4");
    }
}