时间的差值计算

来源:互联网 发布:淘宝自主品牌女装 编辑:程序博客网 时间:2024/05/12 12:59


需求是系统当前的时间与用户输入的时间比较

算出差值(TimeDate是获取用户输入的时间,当然也可以自己写一个固定的时间

Date before = dateFormat .parse("2015-07-01");这样很方便的计算出时间的相差多少天

如果只是比较大小用compareTo即可)

 TimeDate = remaintime.getText().toString();                Date now = new Date();                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");                String curtime = dateFormat.format(now);//把时间转为字符串格式//                System.out.println(now);                Date ctime = null;                Date selecttime = null;                try {                    ctime = dateFormat.parse(curtime);                    selecttime = dateFormat.parse(TimeDate);                    int s = (int) (selecttime.getTime() - ctime.getTime());                    int day= s/(24 * 60 * 60 * 1000);                    System.out.println(day);                } catch (ParseException e) {                    e.printStackTrace();                }