記錄一個(gè)起始時(shí)間,記錄一個(gè)結(jié)束時(shí)間,兩個(gè)相減就是程序運(yùn)行時(shí)間,代碼如下java
創(chuàng)新互聯(lián)是一家從事企業(yè)網(wǎng)站建設(shè)、網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、行業(yè)門戶網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)制作的專業(yè)網(wǎng)站建設(shè)公司,擁有經(jīng)驗(yàn)豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁(yè)設(shè)計(jì)人員,具備各種規(guī)模與類型網(wǎng)站建設(shè)的實(shí)力,在網(wǎng)站建設(shè)領(lǐng)域樹立了自己獨(dú)特的設(shè)計(jì)風(fēng)格。自公司成立以來(lái)曾獨(dú)立設(shè)計(jì)制作的站點(diǎn)近1000家。
怎么計(jì)算程序運(yùn)行的時(shí)間
使用 java.util.TimerTask 類,開啟一個(gè)線程。
創(chuàng)建一個(gè) class MyClass extends TimerTask
實(shí)現(xiàn) run() 方法,其中打印出當(dāng)前時(shí)間。
然后在main方法中 new 一個(gè) MyClass
然后:
Timer timer = new Timer();
TimerTask task = MyClass ;
timer.schedule(task, 0, 1*1000);
就會(huì)看到每秒打印一個(gè)時(shí)間了。
1、首先需要在記事本中編寫一個(gè)“hello,下午好”程序。
2、編寫完成后,保存該文件,并將文件名改為與類名相同。
3、把文件的格式從.txt改成.java文件。
4、更改完畢后,打開cmd指令(wins+R)。
5、輸入javac,如果下方出現(xiàn)許多東西,則說(shuō)明環(huán)境變量已經(jīng)配置成功,否則就要去配置環(huán)境變量。
6、找到.java文件所在的位置,也在cmd中找到它(D盤)。
7、在cmd中進(jìn)入這個(gè).java文件所在的文件夾,輸入javac? ?類名.java ,此時(shí)在.java文件所存在的地方出現(xiàn)同名.class文件。
8、再輸入java? 類名,下方便會(huì)運(yùn)行出.java文件,輸出“hello,下午好”。
有兩種方法:
方法一:用java.util.Date類來(lái)實(shí)現(xiàn),并結(jié)合java.text.DateFormat類來(lái)實(shí)現(xiàn)時(shí)間的格式化,看下面代碼:
mport?java.util.*;?
import?java.text.*;
//以下默認(rèn)時(shí)間日期顯示方式都是漢語(yǔ)語(yǔ)言方式
//一般語(yǔ)言就默認(rèn)漢語(yǔ)就可以了,時(shí)間日期的格式默認(rèn)為MEDIUM風(fēng)格,比如:2008-6-16?20:54:53
//以下顯示的日期時(shí)間都是再Date類的基礎(chǔ)上的來(lái)的,還可以利用Calendar類來(lái)實(shí)現(xiàn)見類TestDate2.java
public?class?TestDate?{?
public?static?void?main(String[]?args)?{?
Date?now?=?new?Date();?
DateFormat?d1?=?DateFormat.getDateInstance();?//默認(rèn)語(yǔ)言(漢語(yǔ))下的默認(rèn)風(fēng)格(MEDIUM風(fēng)格,比如:2008-6-16?20:54:53)
String?str1?=?d1.format(now);
DateFormat?d2?=?DateFormat.getDateTimeInstance();?
String?str2?=?d2.format(now);?
DateFormat?d3?=?DateFormat.getTimeInstance();?
String?str3?=?d3.format(now);?
DateFormat?d4?=?DateFormat.getInstance();?//使用SHORT風(fēng)格顯示日期和時(shí)間
String?str4?=?d4.format(now);
DateFormat?d5?=?DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);?//顯示日期,周,時(shí)間(精確到秒)
String?str5?=?d5.format(now);
DateFormat?d6?=?DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);?//顯示日期。時(shí)間(精確到秒)
String?str6?=?d6.format(now);
DateFormat?d7?=?DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);?//顯示日期,時(shí)間(精確到分)
String?str7?=?d7.format(now);
DateFormat?d8?=?DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);?//顯示日期,時(shí)間(精確到分)
String?str8?=?d8.format(now);//與SHORT風(fēng)格相比,這種方式最好用
System.out.println("用Date方式顯示時(shí)間:?"?+?now);//此方法顯示的結(jié)果和Calendar.getInstance().getTime()一樣
System.out.println("用DateFormat.getDateInstance()格式化時(shí)間后為:"?+?str1);
System.out.println("用DateFormat.getDateTimeInstance()格式化時(shí)間后為:"?+?str2);
System.out.println("用DateFormat.getTimeInstance()格式化時(shí)間后為:"?+?str3);
System.out.println("用DateFormat.getInstance()格式化時(shí)間后為:"?+?str4);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時(shí)間后為:"?+?str5);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時(shí)間后為:"?+?str6);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時(shí)間后為:"?+?str7);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時(shí)間后為:"?+?str8);
}
}
運(yùn)行結(jié)果:
用Date方式顯示時(shí)間: Thu Sep 17 09:39:46 CST 2015
用DateFormat.getDateInstance()格式化時(shí)間后為:2015-9-17
用DateFormat.getDateTimeInstance()格式化時(shí)間后為:2015-9-17 9:39:46
用DateFormat.getTimeInstance()格式化時(shí)間后為:9:39:46
用DateFormat.getInstance()格式化時(shí)間后為:15-9-17 上午9:39
用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化時(shí)間后為:2015年9月17日 星期四 上午09時(shí)39分46秒 CST
用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化時(shí)間后為:2015年9月17日 上午09時(shí)39分46秒
用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化時(shí)間后為:15-9-17 上午9:39
用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時(shí)間后為:2015-9-17 9:39:46
方法二:用java.util.Calendar類來(lái)實(shí)現(xiàn),看下面:
import?java.util.*;?
import?java.text.*;
//以下是利用Calendar類來(lái)實(shí)現(xiàn)日期時(shí)間的,和Date類相比較比較簡(jiǎn)單
public?class?TestDate2?{?
public?static?void?main(String[]?args)?{?
Calendar?ca?=?Calendar.getInstance();
int?year?=?ca.get(Calendar.YEAR);//獲取年份
int?month=ca.get(Calendar.MONTH);//獲取月份?
int?day=ca.get(Calendar.DATE);//獲取日
int?minute=ca.get(Calendar.MINUTE);//分?
int?hour=ca.get(Calendar.HOUR);//小時(shí)?
int?second=ca.get(Calendar.SECOND);//秒
int?WeekOfYear?=?ca.get(Calendar.DAY_OF_WEEK);?
System.out.println("用Calendar.getInstance().getTime()方式顯示時(shí)間:?"?+?ca.getTime());
System.out.println("用Calendar獲得日期是:"?+?year?+"年"+?month?+"月"+?day?+?"日");
System.out.println("用Calendar獲得時(shí)間是:"?+?hour?+"時(shí)"+?minute?+"分"+?second?+"秒");
System.out.println(WeekOfYear);//顯示今天是一周的第幾天(我做的這個(gè)例子正好是周二,故結(jié)果顯示2,如果你再周6運(yùn)行,那么顯示6)
}
}
運(yùn)行結(jié)果是:
用Calendar.getInstance().getTime()方式顯示時(shí)間: Thu Sep 17 09:40:40 CST 2015
用Calendar獲得日期是:2015年8月17日
用Calendar獲得時(shí)間是:9時(shí)40分40秒
5
總結(jié):中的來(lái)說(shuō),方法二是最方便的,方法一顯得分笨拙,不過(guò)看個(gè)人喜歡了。
開始運(yùn)行程序前,你定義一個(gè)變量去保存開始的時(shí)間,在程序結(jié)束的時(shí)候,你可以再定義一個(gè)變量去保存這個(gè)時(shí)間,其實(shí)你可以用Double來(lái)存取,時(shí)間很短的,過(guò)后相減一下就可以得出來(lái)了。
給你個(gè)例子吧:
public class Test {
public static void main(String[] args) {
double start = System.currentTimeMillis() ;
for( int i = 0 ; i 100000 ; i ++){
System.out.println("") ;
}
double end = System.currentTimeMillis() ;
System.out.println("time is : " + (end - start));
}
}
程序開始時(shí)開始計(jì)時(shí),
有個(gè)long time1 = System。getCurrent()什么的方法吧,
毫秒的
程序結(jié)束之前再執(zhí)行下
long time2 = System。getCurrent()
那么你的程序運(yùn)行時(shí)間就是time2-time1
分享標(biāo)題:保存java代碼運(yùn)行日期,java 文件創(chuàng)建時(shí)間
URL分享:http://aaarwkj.com/article30/dsiscso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、電子商務(wù)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)