java Calendar日期相减问题

两个日期相减,必须是Date型变量使用其getTime()后相减,所以如果是从Calendar获得时间,则需:
Calendar date1 = Calendar.getInstance();
Calendar date2 = Calendar.getInstance();

date1.getTime().getTime() - date2.getTime().getTime() ;

结果为long型,毫秒!
实例1:

	Calendar cal=Calendar.getInstance();
		cal.setTime(new java.util.Date());
		Calendar calendar=Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY,21);
		calendar.set(Calendar.MINUTE,30);
		calendar.set(Calendar.SECOND,00);
		StringBuffer stb=new StringBuffer();
		long between=(calendar.getTime().getTime()-cal.getTime().getTime())/1000;
		long hour=between/3600;   
		long minute=between%3600/60;   
	    long second=between%60;
		stb.append(+hour);
		stb.append("时");
		if(minute<10){
			stb.append("0");
		}
		stb.append(minute);
		stb.append("分");
		if(second<10){
			stb.append("0");
		}
		stb.append(second);
		stb.append("秒");
		


你可能感兴趣的:(java)