Java獲取當前系統時間,以及計算時間差,返回毫秒差值

//獲取當前時間
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;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章