当前位置:首页 » 编程语言 » java运行时间

java运行时间

发布时间: 2022-04-24 02:23:17

java 怎么计算程序运行的时间

long a=System.currentTimeMillis();
long b=System.currentTimeMillis();
a放在程序的开始
b放在结束,然后相减即是程序运行时间

㈡ 如何检测一个JAVA程序的运行时间

检测一个JAVA程序的运行时间方法:

longstartTime=System.currentTimeMillis();//获取当前时间
//doSomeThing();//要运行的java程序
longendTime=System.currentTimeMillis();
System.out.println("程序运行时间:"+(endTime-startTime)+"ms");

㈢ 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


总结:中的来说,方法二是最方便的,方法一显得分笨拙,不过看个人喜欢了。

㈣ java 如何计算线程执行时间

线程是独立的,启动了,就不受控,你可以考虑下面的方式,虽然不优雅,不过能解决你的问题,而跟线程相关的,也只能这样来设计。
(如果你觉得线程的不太优雅,你可以采用观察者的模式来加在一起,那代码就优雅多了,简单的问题,不建议搞得这么优雅,你觉得呢?)

启动线程之前,获取系统时间,做为开始时间
线程结束后,在线程的代码的最后再获取一次系统时间,这个时间减去刚刚上面的那个时间,就是运算时间

海枫科技、海枫论坛

㈤ 计算Java程序执行时间的几种方法

最简单的就是直接获取开始时间、结束时间,计算时间差

publicclassDemo{
publicstaticvoidmain(String[]args){
longstartTime=System.currentTimeMillis();//获取开始时间

doSomething();//测试的代码段

longendTime=System.currentTimeMillis();//获取结束时间

System.out.println("程序运行时间:"+(endTime-startTime)+"ms");//输出程序运行时间
}
}

㈥ Java方法的运行时间

publicstaticvoidmain(String[]args){
longstart=System.currentTimeMillis();
A();
System.out.println(System.currentTimeMillis()-start);
}

㈦ 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+"秒");

}
}

运行结果如下:

㈧ 如何计算程序运行时间JAVA

main入口时
date
d
=
new
date()
所有运算结束时
date
d1
=
new
date();
long
hm
=
d1.gettime()-d.gettime()
//取得相差毫秒数
之后输出
hm/1000
输出的是运行了多少秒(分钟
小时什么的自己应该会换算了吧?)
如果想复杂一点
可以写一个监视线程
随main启动
同样记录一个启动时间
判断main线程被终止时记录程序停止时间
随后算法同上

㈨ 有一个Java程序,我想知道它的运行时间,怎样把代码加进去啊

程序开始时开始计时,
有个long
time1
=
System。getCurrent()什么的方法吧,
毫秒的
程序结束之前再执行下
long
time2
=
System。getCurrent()
那么你的程序运行时间就是time2-time1

㈩ java如何计算程序运行时间

第一种是以毫秒为单位计算的。

[java]view plain

//伪代码

longstartTime=System.nanoTime();//获取开始时间

doSomeThing();//测试的代码段

longendTime=System.nanoTime();//获取结束时间

System.out.println("程序运行时间:"+(end-start)+"ns");

包含所需的包: import java.lang.System;

热点内容
看我QQ密码多少 发布:2025-05-17 15:27:12 浏览:264
我配置很高了ae为什么卡 发布:2025-05-17 14:54:50 浏览:169
python数据分析实战pdf 发布:2025-05-17 14:49:42 浏览:952
海澜之家广告脚本 发布:2025-05-17 13:56:06 浏览:34
手文件夹恢复 发布:2025-05-17 13:53:32 浏览:997
linux怎么看进程 发布:2025-05-17 13:53:30 浏览:307
thinkphp字段缓存 发布:2025-05-17 13:52:01 浏览:579
山灵app安卓版如何设置 发布:2025-05-17 13:51:49 浏览:392
帆布压缩袋 发布:2025-05-17 13:26:27 浏览:462
c语言16进制表示方法 发布:2025-05-17 13:11:25 浏览:484