//獲取當前時間 System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS").format(new Date())); /** * 根據String型時間,獲取long型時間,單位毫秒 * @param inVal 時間字符串 * @return long型時間 */ public static long fromDateStringToLong(String inVal) { Date date = null; SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS"); try { date = inputFormat.parse(inVal); } catch (Exception e) { e.printStackTrace(); } return date.getTime(); }//獲取當前時間爲截止時間,轉換爲long型
long stopTime = fromDateStringToLong(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS").format(new Date()));
//獲取當前時間爲開始時間,轉換爲long型
long startTime =fromDateStringToLong(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS").format(new Date()));
//計算時間差,單位毫秒
long timeSpan = stopTime - startTime;