1、時間轉換
data默認有toString() 輸出格林威治時間,比如說Date date = new Date(); String toStr = date.toString(); 輸出的結果類似於: Wed Sep 16 19:02:36 CST 2012 你要輸出yyyy-MM-dd hh:mm:ss這種格式的話, 使用SimpleDataFormat類 比如 Date date = new Date(); String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date); System.out.println(dateStr); 輸出結果像下面這樣: 2009-09-16 07:02:36當然啦,你也可以把:hh:mm:ss去掉,輸出的結果也就只有年-月-日了
2、時間差
public static String getDatePoor(Date endDate, Date nowDate) { long nd = 1000 * 24 * 60 * 60; long nh = 1000 * 60 * 60; long nm = 1000 * 60; // long ns = 1000; // 獲得兩個時間的毫秒時間差異 long diff = endDate.getTime() - nowDate.getTime(); // 計算差多少天 long day = diff / nd; // 計算差多少小時 long hour = diff % nd / nh; // 計算差多少分鍾 long min = diff % nd % nh / nm; // 計算差多少秒//輸出結果 // long sec = diff % nd % nh % nm / ns; return day + "天" + hour + "小時" + min + "分鍾"; }