JAvaで計算する2つの日付の差は数日です

1045 ワード

public class Test {
public void dateDiff(String startTime, String endTime, String format) {
//           simpledateformate  
SimpleDateFormat sd = new SimpleDateFormat(format);
long nd = 1000*24*60*60;//      
long nh = 1000*60*60;//       
long nm = 1000*60;//       
long ns = 1000;//       
long diff;
try {
//             
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
long day = diff/nd;//      
long hour = diff%nd/nh;//       
long min = diff%nd%nh/nm;//       
long sec = diff%nd%nh%nm/ns;//      
//    
System.out.println("    :"+day+" "+hour+"  "+min+"  "+sec+" 。");
} catch (ParseException e) {
e.printStackTrace();
}
}


public static void main(String[] args) {
new Test().dateDiff(new SimpleDateFormat("yyyy-MM-dd").format(new Date()), "2010-8-23", "yyyy-MM-dd");
}
}