判断日期距离现在几个月

Integer monthNum = monthCompare("2019-12-01");
   /**
     * 传入参数返回距离现在几个月
     */
    public static Integer monthCompare(String oldTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date nowDate = new Date();
        String nowDateStr = sdf.format(nowDate);


        LocalDate from = LocalDate.parse(oldTime);
        LocalDate to = LocalDate.parse(nowDateStr);

        Integer months = Math.toIntExact(ChronoUnit.MONTHS.between(from, to));

        return months;
    }

 

上一篇:C语言获取字符年月日时分秒毫秒


下一篇:jQuery 获取系统当前时间