1、计算两个日期之间的分钟数:
class="java">Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); System.out.println(calendar.getTimeInMillis()); SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date ds = d.parse("2013-12-12 12:12:12"); Date d2 = d.parse("2013-12-12 12:15:12"); System.out.println((d2.getTime()-ds.getTime())/60/1000);
?2、比较两个日期
String s1 = "2013-01-12 12:01:01"; Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); try { c1.setTime(sdf.parse(s1)); c2.setTime(date); System.out.println(c1.compareTo(c2)); //输出-1,说明给点时间早于当前时间,若是1则表示大于当前时间 } catch (ParseException e) { e.printStackTrace(); }
?
?