java时间差计算

    private static String FORMAT_DAY = "yyyy-MM-dd";

/**
     *  计算得到时间差,单位:天   注:不含结束日期  2019-02-21~2019-02-23  结果2
     * @param startDate 开始日期
     * @param endDate  结束日期
     * @return
     */
    public static int dateDifference(String startDate, String endDate)   {
        Date d1 = conversionToDate(startDate,FORMAT_DAY);
        Date d2 = conversionToDate(endDate,FORMAT_DAY);
        long dateDifference = d2.getTime() - d1.getTime();
        long nd = 1000 * 24 * 60 * 60;
        return (int)(dateDifference / nd);
    }
 //格式化时间
    public static Date conversionToDate(String date, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date d = null;
        try {
            d = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

猜你喜欢

转载自blog.csdn.net/qq_38974073/article/details/88352556
今日推荐