獲取方法執行時間 -System.currentTimeMillis();

一、代碼javascript

public class TimeTest {

    /**
     * 計算兩個時間點直接逝去的毫秒數
     *
     */
    public void computeAndDisplayElapsedTime() {
        //定義開始時間
        long startTime = System.currentTimeMillis();//獲取當前時間


        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(60);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        //定義結束時間
        long endTime = System.currentTimeMillis();


        float seconds = (endTime - startTime) / 1000F;

        System.out.println(Float.toString(seconds) + " seconds.");

    }

    /**
     * 啓動程序
     */
    public static void main(String[] args) {
        new TimeTest().computeAndDisplayElapsedTime();
    }
}
java

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////spa

二、知識操作系統

System類表明系統,系統級的不少屬性和控制方法都放置在該類的內部。該類位於java.lang包。code

currentTimeMillis方法orm

public static long currentTimeMillis()ip

該方法的做用是返回當前的計算機時間,時間的表達格式爲當前計算機時間和GMT時間(格林威治時間)1970年1月1號0時0分0秒所差的毫秒數。get

能夠直接把這個方法強制轉換成date類型。it

代碼以下:io

long currentTime = System.currentTimeMillis();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH時mm分ss秒");

Date date = new Date(currentTime);

System.out.println(formatter.format(date));

運行結果以下:

當前時間:2011年-08月10日-14時11分46秒

另:

可得到當前的系統和用戶屬性:

       String osName = System.getProperty(「os.name」);

  String user = System.getProperty(「user.name」);

  System.out.println(「當前操做系統是:」 + osName);

  System.out.println(「當前用戶是:」 + user);

       System.getProperty 這個方法能夠獲得不少系統的屬性。

//////////////////////////////////////////////////////////////////////////////////////////////////////////

三、js

<script type="text/javascript">
var Date1 = new Date();
var Date2=new Date("00:00:00 5/1/1900");
var gotime=Date1.getTime()-Date2.getTime();
alert("兩個時間相差"+gotime+"毫秒");
</script>

也能夠用 
var Date1 = new Date();
Date1.getMilliseconds();
相關文章
相關標籤/搜索