導航:首頁 > 編程語言 > java記錄運行時間

java記錄運行時間

發布時間:2024-04-03 06:26:30

1. java 如何獲取系統運行時間

有兩種方法:

方法一:用java.util.Date類來實現,並結合java.text.DateFormat類來實現時間的格式化,看下面代碼

mportjava.util.*;
importjava.text.*;
//以下默認時間日期顯示方式都是漢語語言方式
//一般語言就默認漢語就可以了,時間日期的格式默認為MEDIUM風格,比如:2008-6-1620:54:53
//以下顯示的日期時間都是再Date類的基礎上的來的,還可以利用Calendar類來實現見類TestDate2.java
publicclassTestDate{
publicstaticvoidmain(String[]args){
Datenow=newDate();
DateFormatd1=DateFormat.getDateInstance();//默認語言(漢語)下的默認風格(MEDIUM風格,比如:2008-6-1620:54:53)
Stringstr1=d1.format(now);
DateFormatd2=DateFormat.getDateTimeInstance();
Stringstr2=d2.format(now);
DateFormatd3=DateFormat.getTimeInstance();
Stringstr3=d3.format(now);
DateFormatd4=DateFormat.getInstance();//使用SHORT風格顯示日期和時間
Stringstr4=d4.format(now);
DateFormatd5=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);//顯示日期,周,時間(精確到秒)
Stringstr5=d5.format(now);
DateFormatd6=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);//顯示日期。時間(精確到秒)
Stringstr6=d6.format(now);
DateFormatd7=DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);//顯示日期,時間(精確到分)
Stringstr7=d7.format(now);
DateFormatd8=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//顯示日期,時間(精確到分)
Stringstr8=d8.format(now);//與SHORT風格相比,這種方式最好用

System.out.println("用Date方式顯示時間:"+now);//此方法顯示的結果和Calendar.getInstance().getTime()一樣


System.out.println("用DateFormat.getDateInstance()格式化時間後為:"+str1);
System.out.println("用DateFormat.getDateTimeInstance()格式化時間後為:"+str2);
System.out.println("用DateFormat.getTimeInstance()格式化時間後為:"+str3);
System.out.println("用DateFormat.getInstance()格式化時間後為:"+str4);

System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時間後為:"+str5);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時間後為:"+str6);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時間後為:"+str7);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間後為:"+str8);
}
}

運行結果:

用Date方式顯示時間: Thu Sep 17 09:39:46 CST 2015


用DateFormat.getDateInstance()格式化時間後為:2015-9-17


用DateFormat.getDateTimeInstance()格式化時間後為:2015-9-17 9:39:46


用DateFormat.getTimeInstance()格式化時間後為:9:39:46


用DateFormat.getInstance()格式化時間後為:15-9-17 上午9:39


用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時間後為:2015年9月17日 星期四 上午09時39分46秒 CST


用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時間後為:2015年9月17日 上午09時39分46秒


用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時間後為:15-9-17 上午9:39


用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間後為:2015-9-17 9:39:46

方法二:用java.util.Calendar類來實現,看下面:

importjava.util.*;
importjava.text.*;
//以下是利用Calendar類來實現日期時間的,和Date類相比較比較簡單
publicclassTestDate2{
publicstaticvoidmain(String[]args){

Calendarca=Calendar.getInstance();
intyear=ca.get(Calendar.YEAR);//獲取年份
intmonth=ca.get(Calendar.MONTH);//獲取月份
intday=ca.get(Calendar.DATE);//獲取日
intminute=ca.get(Calendar.MINUTE);//分
inthour=ca.get(Calendar.HOUR);//小時
intsecond=ca.get(Calendar.SECOND);//秒
intWeekOfYear=ca.get(Calendar.DAY_OF_WEEK);


System.out.println("用Calendar.getInstance().getTime()方式顯示時間:"+ca.getTime());
System.out.println("用Calendar獲得日期是:"+year+"年"+month+"月"+day+"日");

System.out.println("用Calendar獲得時間是:"+hour+"時"+minute+"分"+second+"秒");
System.out.println(WeekOfYear);//顯示今天是一周的第幾天(我做的這個例子正好是周二,故結果顯示2,如果你再周6運行,那麼顯示6)

}
}

運行結果是:

用Calendar.getInstance().getTime()方式顯示時間: Thu Sep 17 09:40:40 CST 2015

用Calendar獲得日期是:2015年8月17日

用Calendar獲得時間是:9時40分40秒

5


總結:中的來說,方法二是最方便的,方法一顯得分笨拙,不過看個人喜歡了。

2. java如何記錄方法運行時間

//不需要導入包
//在你的方法第一行加上:
long a=System.currentTimeMillis();
//在最好的版一行加上:
System.out.println("\r<br>執行權耗時 : "+(System.currentTimeMillis()-a)/1000f+" 秒 ");

3. JAVA 獲取一段程序運行時間

abstractclassGetTime{
oidgetTime(){
longstart=System.currentTimeMillis();
runcode();
longend=System.currentTimeMillis();
System.out.println("運行時間:"+(end-start)+"毫秒");//應該是end-start
}

publicabstractvoidruncode();
}

{//建立一個java文件為SubTime.java,SubTime為主類,加為public
publicvoidruncode(){
for(intx=0;x<4000;x++){
System.out.println(x);
}
}

staticpublicvoidmain(Stringargs[]){//寫一個主函數就好了
newSubTime().getTime();//建立對象調用getTime();
}
}

可以用eclipse運行

4. 如何檢測一個JAVA程序的運行時間

檢測一個JAVA程序的運行時間方法:

longstartTime=System.currentTimeMillis();//獲取當前時間
//doSomeThing();//要運行的java程序版
longendTime=System.currentTimeMillis();
System.out.println("程序運行時間:權"+(endTime-startTime)+"ms");

5. 如何得到java程序運行花了多少時間

記錄一個起始時間,記錄一個結束時間,兩個相減就是程序運行時間,代碼如下

longstart=System.currentTimeMillis();	//記錄起專始時間
try{
Thread.sleep(5000); 屬 //線程睡眠5秒,讓運行時間不那麼小
}catch(InterruptedExceptione){
e.printStackTrace();
}
longend=System.currentTimeMillis(); //記錄結束時間
System.out.println(end-start); //相減得出運行時間

得出的單位是毫秒。

6. java 如何獲取應用的運行時間

java獲取應用的運行時間,可以利用時間差來獲得,使用System.currentTimeMillis()該方法獲得此時的時間版,代碼如下:

packagecom.qiu.lin.he;

importjava.text.ParseException;

publicclassCeshi{
publicstaticvoidmain(String[]args)throwsParseException{

doublebegin=System.currentTimeMillis();//程序權開始時間,調用系統的當前時間
for(inti=0;i<10000;i++){
//這里執行具體的業務邏輯
System.out.println(i);
}
//你要運行的程序
doubleend=System.currentTimeMillis();//程序結束時間,調用系統當前時間
doubletime=end-begin;//程序的運行時間

System.out.println(time/60+"秒");

}
}

運行結果如下:

7. java如何計算程序運行時間

long begin = System.currentTimeMillis(); // 這段代碼放在程序執行前

long end = System.currentTimeMillis() - begin; // 這段代碼放在程序執行後專
System.out.println("耗時:" + end + "毫秒屬");

閱讀全文

與java記錄運行時間相關的資料

熱點內容
如何知道櫥窗賣出的商品是哪個app的 瀏覽:56
大眾寶來點火線圈數據流通道號是多少 瀏覽:981
手機後台網路怎麼關閉 瀏覽:537
大數據安徽 瀏覽:563
iphone鎖屏密碼能破解嗎 瀏覽:964
電信運營商大數據徵信 瀏覽:699
怎麼u盤里的文件打不開 瀏覽:97
如何發word文件怎麼打開 瀏覽:176
惠普還原系統win10教程 瀏覽:167
iphone6ssunspider 瀏覽:796
java獲取攝像頭 瀏覽:959
怎麼用網線傳文件 瀏覽:24
電導增量法matlab程序 瀏覽:366
手機文件管理在那裡 瀏覽:205
如何取消卸載app的續費 瀏覽:316
數控編程哪個最容易 瀏覽:170
光速互動官方教程 瀏覽:411
谷歌登陸網站打不開怎麼辦 瀏覽:937
java什麼是非同步編程 瀏覽:898
怎麼刪除qq裡面的文件 瀏覽:503

友情鏈接