需要做一個時間差的判斷,有點不想寫,於是翻以前的代碼發現,米有….現在來記錄一下吧
public static void main(String[] args) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentTime=new Date();
//將截取到的時間字符串轉化為時間格式的字符串
Date beginTime=sdf.parse("2011-09-14 12:53:30");
//默認為毫秒,除以1000是為瞭轉換成秒
long interval=(currentTime.getTime()-beginTime.getTime())/1000;//秒
long day=interval/(24*3600);//天
long hour=interval%(24*3600)/3600;//小時
long minute=interval%3600/60;//分鐘
long second=interval%60;//秒
System.out.println("兩個時間相差:"+day+"天"+hour+"小時"+minute+"分"+second+"秒");
}
作者“xingcxb”