经常使用下面这两个时间值当唯一标识,却总记不住它们的时间范围,记录之。
[code]
System.currentTimeMillis();
System.nanoTime();
[/code]

其中currentTimeMillis的时间范围可以简单代码测试得到。
先看描述
[code]
long java.lang.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 Date for 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:
java.util.Date
[/code]
再看代码
[code]
Calendar c = Calendar.getInstance();
c.setTimeInMillis(Long.MAX_VALUE);
System.out.println(c.get(Calendar.YEAR));
[/code]
输入结果为:292278994,加上几千分符292,278,994 = 0.29billion
单位是年,将三亿年,咱们肯定都活不到那时候了~ 人类那时候还在不在也不好说了~ 应该够用了~~

再看nanoTime的描述
[code]
long java.lang.System.nanoTime()

Returns the current value of the running Java Virtual Machine's high-resolution time source, 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 origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.

This method provides nanosecond precision, but not necessarily nanosecond resolution (that is, how frequently the value changes) - no guarantees are made except that the resolution is at least as good as that of currentTimeMillis().

Differences in successive calls that span greater than approximately 292 years (2^63 nanoseconds) will not correctly compute elapsed time due to numerical overflow.

The values returned by this method become meaningful only when the difference between two such values, obtained within the same instance of a Java virtual machine, is computed.

For example, to measure how long some code takes to execute:

long startTime = System.nanoTime();
// ... the code being measured ...
long estimatedTime = System.nanoTime() - startTime;
To compare two nanoTime values

long t0 = System.nanoTime();
...
long t1 = System.nanoTime();
one should use t1 - t0 < 0, not t1 < t0, because of the possibility of numerical overflow.
Returns:
the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds
Since:
1.5
[/code]
人家已经说得很清楚,区间为292年。还是算一下能用到哪一年吧。
[code]
long nanosToMax = Long.MAX_VALUE - System.nanoTime();// 还剩下的nanos数
long millisToMax = TimeUnit.NANOSECONDS.toMillis(nanosToMax);// 转为剩下的毫秒数
long maxMillis = System.currentTimeMillis() + millisToMax;// 根据当前时间算出的最大nano对应的millis时间
Calendar c = Calendar.getInstance();
c.setTimeInMillis(maxMillis);
System.out.println(c.get(Calendar.YEAR));
[/code]
运行结果是:2308
2308 - 2016 = 292
这个数字居然跟人家描述里说得一样,总觉得哪不对~ 好吧,还有两百多年可以用,我们也都不会见到那一刻了~

标签: currentTimeMillis, Java, nanoTime

添加新评论