关于ZAKER 融媒体解决方案 合作 加入

Java 中有秒表吗?

CocoaChina 09-20

Java中有秒表吗?在谷歌我只找到不起作用的秒表代码 – 它们总是返回 0 毫秒 .

我找到的这段代码不起作用 , 但我不明白为什么 .

public class StopWatch { private long startTime = 0; private long stopTime = 0; private boolean running = false; public void start ( ) { this.startTime = System.currentTimeMillis ( ) ; this.running = true; } public void stop ( ) { this.stopTime = System.currentTimeMillis ( ) ; this.running = false; } //elaspsed time in milliseconds public long getElapsedTime ( ) { long elapsed; if ( running ) { elapsed = ( System.currentTimeMillis ( ) - startTime ) ; } else { elapsed = ( stopTime - startTime ) ; } return elapsed; } //elaspsed time in seconds public long getElapsedTimeSecs ( ) { long elapsed; if ( running ) { elapsed = ( ( System.currentTimeMillis ( ) - startTime ) / 1000 ) ; } else { elapsed = ( ( stopTime - startTime ) / 1000 ) ; } return elapsed; }}

最佳答案

以上内容由"CocoaChina"上传发布 查看原文
相关标签 java谷歌