开发者

Poor performance when using the Calendar API in Java

开发者 https://www.devze.com 2023-02-27 06:34 出处:网络
The following code is givi开发者_JS百科ng very bad performance. I\'ve come to know that there are performance problems with the Calendar API

The following code is givi开发者_JS百科ng very bad performance. I've come to know that there are performance problems with the Calendar API

long startInMilliSec = measFileCreationTime_OssTime;
Calendar thisMoment = Calendar.getInstance();
long currentInMilliSec = thisMoment.getTime().getTime();

if (currentInMilliSec >= startInMilliSec) {
    return (true);
} else {
    return (false);
}

Is there any alternative for this?


you can use System.currentTimeMillis() to get the current time in millis since Dec, 1st 1970. Subtracting two millis values can be used to get the duration in millis between them. Calendar needs only to be used when interpreting the year, Month etc of such a milli value.

If you want to be even more precise, then you could use System.nanoTime() to get nano precision. The absolute nano value has no particular meaning in this case and can only be used for calculating time differences between subsequently taken nano values.

0

精彩评论

暂无评论...
验证码 换一张
取 消