Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.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:加速和减慢时间_Java_Java.util.date - Fatal编程技术网

Java:加速和减慢时间

Java:加速和减慢时间,java,java.util.date,Java,Java.util.date,我试图找出设置任意时间的逻辑,然后让时间以不同的速度“回放”(比如.5x或4x实时) 以下是我到目前为止的逻辑,它将以正常速度播放时间: import java.util.Calendar; public class Clock { long delta; private float speed = 1f; public Clock(Calendar startingTime) { delta = System.currentTimeMillis

我试图找出设置任意时间的逻辑,然后让时间以不同的速度“回放”(比如.5x或4x实时)

以下是我到目前为止的逻辑,它将以正常速度播放时间:

import java.util.Calendar;


public class Clock {


    long delta;
    private float speed = 1f;

    public Clock(Calendar startingTime) {
        delta = System.currentTimeMillis()-startingTime.getTimeInMillis();
    }

    private Calendar adjustedTime() {
        Calendar cal = Calendar.getInstance();

        cal.setTimeInMillis(System.currentTimeMillis()-delta);

        return cal;

    }

    public void setPlaybackSpeed(float speed){
        this.speed  = speed;
    }

    public static void main(String[] args){


        Calendar calendar = Calendar.getInstance();
        calendar.set(2010, 4, 4, 4, 4, 4);
        Clock clock = new Clock(calendar);

        while(true){
            System.out.println(clock.adjustedTime().getTime());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }


}

我很难弄清楚在逻辑中需要在哪里使用“speed”属性

以下代码给出了如何设计这样一个时钟的示例,该时钟的内部状态为
双spped
长启动时间
。它公开了一个发布方法
getTime()
,该方法将从1970年1月1日午夜起以毫秒为单位返回调整后的时间。请注意,调整发生在
开始时间之后

计算调整时间的公式很简单。首先用
startTime
减去
currentTimeMillis()
得到实时增量,然后将该值乘以
speed
得到调整后的时间增量,然后将其添加到
startTime
得到结果

public class VariableSpeedClock {

    private double speed;
    private long startTime;

    public VariableSpeedClock(double speed) {
        this(speed, System.currentTimeMillis());
    }

    public VariableSpeedClock(double speed, long startTime) {
        this.speed = speed;
        this.startTime = startTime;
    }

    public long getTime () {
        return (long) ((System.currentTimeMillis() - this.startTime) * this.speed + this.startTime);
    }

    public static void main(String [] args) throws InterruptedException {

        long st = System.currentTimeMillis();
        VariableSpeedClock vsc = new VariableSpeedClock(2.3);

        Thread.sleep(1000);

        System.out.println(vsc.getTime() - st);
        System.out.println(System.currentTimeMillis() - st);

    }
}

实时性和“定制”时钟之间的关系似乎很简单。也就是说,在现实世界中,每过一秒,你的时钟就会滴答作响
速度
秒<代码>T_时钟=速度*T_真实值
。剩下的就是你如何定义你的时钟接口。如果你只需要毫秒计时,日历就太贵了。我会使用
long
System.currentTimeMillis()