其他分享
首页 > 其他分享> > 判断日期距离现在几个月

判断日期距离现在几个月

作者:互联网

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;
    }

 

标签:判断,String,距离,SimpleDateFormat,日期,nowDate,Integer,oldTime,LocalDate
来源: https://blog.csdn.net/qq_24023751/article/details/114118793