Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使System.currentTimeMillis()停止计数?_Java_Time - Fatal编程技术网

Java 使System.currentTimeMillis()停止计数?

Java 使System.currentTimeMillis()停止计数?,java,time,Java,Time,在我的代码中,我有一个在顶部运行的计时器,其值为System.currentTimeMillis()/1000.0。游戏开始时,名为tStart的值设置为System.currentTimeMillis,游戏结束时,名为tEnd的值设置为System.currentTimeMillis()。游戏结束时,时间值将从System.currentTimeMillis()更改为tEnd-tStart,但它不会停留在一个值上,而是继续计数。如何停止它?您不能停止系统时钟。 您可以找到第三方秒表类,它们可以

在我的代码中,我有一个在顶部运行的计时器,其值为
System.currentTimeMillis()/1000.0
。游戏开始时,名为
tStart
的值设置为
System.currentTimeMillis
,游戏结束时,名为
tEnd
的值设置为
System.currentTimeMillis()
。游戏结束时,时间值将从
System.currentTimeMillis()
更改为
tEnd-tStart
,但它不会停留在一个值上,而是继续计数。如何停止它?

您不能停止系统时钟。 您可以找到第三方秒表类,它们可以做您想做的事情

比如,它有org.apache.commons.lang.time.StopWatch


一般来说,您需要保存tStart和tEnd变量,如果您需要再次启动计时器并将上一圈与新圈关联,只需找到偏移量(tStart2-tEnd)

currentTimeMillis以毫秒为单位显示当前时间。你不能让它停下来,只能读它。
你能展示你的代码吗?这似乎仍然有效。

我想,你应该试着解释一下,你到底想什么时候收到。 出于最简单的目的,您可以使用Stopwatch的这个简单实现(并最终通过所需的功能扩展它的功能):


这个问题很难理解。您能提供一些代码示例吗?欢迎使用SO。不幸的是,我们无法帮助您,除非您向我们提供服务。您不能这样做。您应该将tEnd-tStart的值保存在变量中。欢迎使用SO。这类答案应该作为评论留下。我知道你没有足够的声誉留下评论,但这并不意味着你需要大量发表评论。在文档中添加一些代码和/或链接,使其更像答案。
public class Stopwatch {

    private long startTime;
    private long endTime;
    private boolean running;

    public Stopwatch(){

    }

    public void start(){
        this.startTime = System.nanoTime();
        running = true;
    }

    public void stop(){
        this.endTime = System.nanoTime();
        running = false;
    }

    public long getElapsedTime(){
        return (this.running ? System.nanoTime() : this.endTime) - this.startTime;
    }

    public boolean isRunning(){
        return this.running;
    }

    public void reset(){
        this.startTime = 0L;
        this.endTime = 0L;
    }

}