Java 获取时间间隔的方法
2012-04-17 22:04
148 查看
Java中获取时间间隔的方法比较简单,通过System.currentTimeMillis()函数即可
Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating
system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
See the description of the class
Returns:the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.See Also:
Returns the current value of the most precise available system timer, in nanoseconds.
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so
values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds)
will not accurately compute elapsed time due to numerical overflow.
For example, to measure how long some code takes to execute:
Returns:The current value of the system timer, in nanoseconds.Since:1.5
测试currentTimeMillis函数的性能
final int iLoop = 100000000;
long lbegin = System.currentTimeMillis();
for (int i = 0; i < iLoop; ++i)
{
System.currentTimeMillis();
}
long lend = System.currentTimeMillis();
System.out.printf("cost : %.3f", (double)(lend - lbegin) / 1000);
执行得到的结果约2秒多,和C/C++的gettimeofday相近,所以可以放心使用
cost : 2.776
public static long currentTimeMillis()
Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating
system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.
See the description of the class
Datefor a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC).
Returns:the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.See Also:
Date
nanoTime
public static long nanoTime()
Returns the current value of the most precise available system timer, in nanoseconds.
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so
values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds)
will not accurately compute elapsed time due to numerical overflow.
For example, to measure how long some code takes to execute:
long startTime = System.nanoTime(); // ... the code being measured ... long estimatedTime = System.nanoTime() - startTime;
Returns:The current value of the system timer, in nanoseconds.Since:1.5
测试currentTimeMillis函数的性能
final int iLoop = 100000000;
long lbegin = System.currentTimeMillis();
for (int i = 0; i < iLoop; ++i)
{
System.currentTimeMillis();
}
long lend = System.currentTimeMillis();
System.out.printf("cost : %.3f", (double)(lend - lbegin) / 1000);
执行得到的结果约2秒多,和C/C++的gettimeofday相近,所以可以放心使用
cost : 2.776
相关文章推荐
- Java后端与数据库中获取时间间隔的不同方法
- java根据时间date获取该时间距离现在的时间间隔
- java&nbsp;Calendar方法&nbsp;获取各种时间
- Java获取各种常用时间方法
- Java 以指定日期时间格式获取当前时间、以及每隔一秒刷新一次的方法------附带实例
- Java获取各种常用时间方法
- java后台获取不同时间的方法
- 获取java时间戳的方法
- java获取时间的方法总结
- Java获取各种常用时间方法(工具类)
- java 获取当前时间的4种方法
- Java,.NET中当前时间获取、格式化方法比较
- Java获取当前时间,及转化格式的方法
- java 获取日期和时间方法总结
- JAVA 获取两个时间的间隔
- Java获取UTC时间的方法详解
- Java获取当前时间的年月日方法
- Java获取各种常用时间方法
- Java获取各种常用时间方法
- java获取两个字符串日期之间的时间间隔天数,以及遍历这些天数