`
yjgyjg4
  • 浏览: 115895 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

时间相减

    博客分类:
  • java
阅读更多
package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {
		 public static void main(String []args) throws ParseException{
		  Calendar calendar=Calendar.getInstance();
		  
		  SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		  Date date = sd.parse("2011-03-16 10:00");
		  calendar.setTime(date);
		  
		  long timethis=calendar.getTimeInMillis();

		  Date twoDate = sd.parse("2011-03-15 10:00");
		  calendar.setTime(twoDate);
		  
		  long timeend=calendar.getTimeInMillis();
		  long theday=(timethis-timeend)/1000/60/60;
		  System.out.println(theday+"小时");
		  
		//日期相减算出秒的算法 
		  Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2011-03-16 10:00"); 
		  Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2011-03-15 10:00"); 
		  
		  long l = date1.getTime()-date2.getTime() ; 
		  
		  System.out.println(l/1000/60/60+"小时"); 
		  
		  //日期相减得到相差的日期 
		  long day = (date1.getTime()-date2.getTime())/(24*60*60*1000)>0 ? (date1.getTime()-date2.getTime())/(24*60*60*1000): 
		   (date2.getTime()-date1.getTime())/(24*60*60*1000); 
		  
		  System.out.println(day+"天"); 

		 }
		

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics