两个字符串时间算时间差

SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        Date startDate = null;
        Date endDate = null;
        try {
            startDate=inputFormat.parse("2017-09-01 20:00:00");
            endDate=inputFormat.parse("2017-09-02 20:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //得到两个日期对象的总毫秒数
        long firstDateMilliSeconds = startDate.getTime();
        long secondDateMilliSeconds = endDate.getTime();

        //得到两者之差
        long firstMinusSecond = secondDateMilliSeconds - firstDateMilliSeconds;
        //得到秒
        long totalSeconds = (firstMinusSecond / 1000);

你可能感兴趣的:(java)