Time Difference

I want to get a change in time, but I don’t know how to do that in processing (in java I know there is Instant.Now but what is there in processing)?

1 Like

Function millis() tells you how many milliseconds has passed from start of the sketch.

1 Like

There are a number of ways:

  • System.currentTimeMillis() returns Unix time (time in miliseconds since January 1st 1970 00:00:00)
  • millis() returns time in miliseconds since the start of your sketch
  • System.nanoTime() returns time in nanoseconds. It is only useful for differences in time.
  • Instant.now() returns an Instance object (It might be slow since Instance is an object and object construction is slow)

As for me, I prefer to use System.currentTimeMillis() and I always have a custom Stopwatch class. This is a simple one I just quickly made:

public final class Stopwatch{
  private long lastUpdate;

  public Stopwatch(){
    this.lastUpdate = System.currentTimeMillis();
  }

  public long update(){
    long time = System.currentTimeMillis();
    long delta = this.lastUpdate - time;
    this.lastUpdate = time;
    return delta;
  }
}

Also you should know, Processing is just a library for Java. You can still use your Instant.now() approach. Nothing changes, but almost everything you write in your .pde files gets wrapped in a class that extends PApplet.

1 Like

Hello, thank you for your help, but to still use the Instant.now you have to have set up processing in an IDE and when I did it on intellij it used settings() instead of setup() so I don’t know how easy it would be to write code like that
thanks for the help

also could you please put in the whole stopwatch class not just the simple one because that would be very helpful
thanks

When Processing compiles your code (using an executable processing-java.exe), all .pde files get combined in one .java file and then edited. All import lines are put to the top, everything else is then put in a PApplet extending class. If you write

void setup(){
    size(640, 480);
}

and compile it, Processing will output

public void settings() {  size(640, 480); }

As an example of a sketch you might make:

Test
├─Test.pde
└─SayHello.pde

Test.pde

import java.nio.file.Paths;

SayHello a, b, c, d;

void setup(){
    size(640, 480, P2D);
    surface.setResizable(true);

    a = new SayHello("Kate");
    b = new SayHello("Thomas");
    c = new SayHello("Anna");
    d = new SayHello("Ezekiel");

    SayHello.say(a);
    SayHello.say(a, b);
    SayHello.say(a, b, c);
    SayHello.say(a, b, c, d);
}

void draw(){
    background(23);
}

SayHello.pde

final static class SayHello{
    private String name;

    SayHello(String name){
        this.setName(name);
    }

    public SayHello setName(String name){
        this.name = name == null ? "" : name;
        return this;
    }
    public String getName(){
        return this.name;
    }

    @Override public String toString(){
        return "Hello " + this.name + "!";
    }
    public SayHello say(){
        System.out.println("Hello " + this.name + "!");
        return this;
    }
    public static void say(SayHello... hellos){
        ArrayList<SayHello> valid = new ArrayList<SayHello>();
        for(int i = 0; i < hellos.length; i ++){
            if(hellos[i] != null) if(!valid.contains(hellos[i])) valid.add(hellos[i]);
        }
        if(valid.size() == 0) System.out.println("There is noone to say hello to. :(");
        else{
            String s = "Hello ";
            for(int i = 0; i < valid.size(); i ++){
                if(i == valid.size() - 1 && valid.size() > 1) s += " and ";
                else if(i > 0) s += ", ";
                s += valid.get(i).getName();
            }
            s += ".";
            System.out.println(s);
        }
    }
}

When Processing compiles it, it becomes:
Test
├─out
│ ├─source
│ │ ├─Test.java
│ ├─Test.class
│ ├─Test§SayHello.class
├─Test.pde
└─SayHello.pde

Test.java

import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.nio.file.Paths; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class Test extends PApplet {
    SayHello a, b, c, d;

    public void setup(){
        surface.setResizable(true);

        a = new SayHello("Kate");
        b = new SayHello("Thomas");
        c = new SayHello("Anna");
        d = new SayHello("Ezekiel");

        SayHello.say(a);
        SayHello.say(a, b);
        SayHello.say(a, b, c);
        SayHello.say(a, b, c, d);
    }

    public void draw(){
        background(23);
    }
    final static class SayHello{
        private String name;

        SayHello(String name){
            this.setName(name);
        }

        public SayHello setName(String name){
            this.name = name == null ? "" : name;
            return this;
        }
        public String getName(){
            return this.name;
        }

        @Override public String toString(){
            return "Hello " + this.name + "!";
        }
        public SayHello say(){
            System.out.println("Hello " + this.name + "!");
            return this;
        }
        public static void say(SayHello... hellos){
            ArrayList<SayHello> valid = new ArrayList<SayHello>();
            for(int i = 0; i < hellos.length; i ++){
                if(hellos[i] != null) if(!valid.contains(hellos[i])) valid.add(hellos[i]);
            }
            if(valid.size() == 0) System.out.println("There is noone to say hello to. :(");
            else{
                String s = "Hello ";
                for(int i = 0; i < valid.size(); i ++){
                    if(i == valid.size() - 1 && valid.size() > 1) s += " and ";
                    else if(i > 0) s += ", ";
                    s += valid.get(i).getName();
                }
                s += ".";
                System.out.println(s);
            }
        }
    }
    public void settings() {  size(640, 480, P2D); }
    static public void main(String[] passedArgs) {
        String[] appletArgs = new String[] { "Test" };
        if (passedArgs != null) {
            PApplet.main(concat(appletArgs, passedArgs));
        } else {
            PApplet.main(appletArgs);
        }
    }
}

This is most likely done, so that the code could be then compiled as a regular Java project.

Sure. I wrote it in pure Java though.

Stopwatch.java

import java.util.ArrayList;

public class Stopwatch {
    final private static ArrayList<Stopwatch> instances = new ArrayList<Stopwatch>();
    private long time, lastUpdated, lastDelta;
    private boolean isRunning;

    public Stopwatch(){
        this.time = 0;
        this.lastUpdated = System.currentTimeMillis();
        this.isRunning = true;
        this.lastDelta = 0;
        instances.add(this);
    }
    public Stopwatch(boolean isRunning){
        this.time = 0;
        this.lastUpdated = System.currentTimeMillis();
        this.isRunning = isRunning;
        this.lastDelta = 0;
        instances.add(this);
    }
    public Stopwatch(boolean isRunning, boolean includeInInstanceList){
        this.time = 0;
        this.lastUpdated = System.currentTimeMillis();
        this.isRunning = isRunning;
        this.lastDelta = 0;
        if(includeInInstanceList) instances.add(this);
    }

    //Static for all instances in Stopwatch.instances
    public static void updateAllInstances(){
        for(int i = 0; i < Stopwatch.instances.size(); i ++){
            Stopwatch.instances.get(i).update();
        }
    }
    public static void destroyAllInstances(){
        for(int i = 0; i < Stopwatch.instances.size(); i ++){
            Stopwatch.instances.get(i).destroy();
        }
    }
    public static Stopwatch[] getRunningInstances(){
        ArrayList<Stopwatch> list = new ArrayList<Stopwatch>();
        for(int i = 0; i < Stopwatch.instances.size(); i ++){
            Stopwatch instance = Stopwatch.instances.get(i);
            if(instance.isRunning()) list.add(instance);
        }
        Stopwatch[] returnArray = new Stopwatch[list.size()];
        for(int i = 0; i < returnArray.length; i ++) returnArray[i] = list.get(i);
        return returnArray;
    }
    public static Stopwatch[] getStoppedInstances(){
        ArrayList<Stopwatch> list = new ArrayList<Stopwatch>();
        for(int i = 0; i < Stopwatch.instances.size(); i ++){
            Stopwatch instance = Stopwatch.instances.get(i);
            if(instance.isStopped()) list.add(instance);
        }
        Stopwatch[] returnArray = new Stopwatch[list.size()];
        for(int i = 0; i < returnArray.length; i ++) returnArray[i] = list.get(i);
        return returnArray;
    }
    public static Stopwatch[] getPausedInstances(){
        ArrayList<Stopwatch> list = new ArrayList<Stopwatch>();
        for(int i = 0; i < Stopwatch.instances.size(); i ++){
            Stopwatch instance = Stopwatch.instances.get(i);
            if(instance.isPaused()) list.add(instance);
        }
        Stopwatch[] returnArray = new Stopwatch[list.size()];
        for(int i = 0; i < returnArray.length; i ++) returnArray[i] = list.get(i);
        return returnArray;
    }
    public static Stopwatch[] getNonrunningInstances(){
        ArrayList<Stopwatch> list = new ArrayList<Stopwatch>();
        for(int i = 0; i < Stopwatch.instances.size(); i ++){
            Stopwatch instance = Stopwatch.instances.get(i);
            if(!instance.isRunning()) list.add(instance);
        }
        Stopwatch[] returnArray = new Stopwatch[list.size()];
        for(int i = 0; i < returnArray.length; i ++) returnArray[i] = list.get(i);
        return returnArray;
    }
    
    //Removes object from Stopwatch.instances
    final public void destroy(){
        Stopwatch.instances.remove(this);
    }

    //Updates Stopwatch
    final public synchronized Stopwatch update(){
        if(!this.isRunning) return this;
        long time = System.currentTimeMillis();
        long delta = this.time - this.lastUpdated;
        this.lastDelta = delta;
        this.time += delta;
        this.lastUpdated = time;
        return this;
    }
    //Updates Stopwatch by a specified time
    final public synchronized Stopwatch update(long time){
        if(!this.isRunning || time <= 0) return this;
        long currentTime = System.currentTimeMillis();
        long delta = time;
        this.lastDelta = delta;
        this.time += delta;
        this.lastUpdated = currentTime;
        return this;
    }
    final public synchronized Stopwatch update(TimeMeasurement time){
        if(!this.isRunning || time == null) return this;
        if(time.value <= 0) return this;
        long currentTime = System.currentTimeMillis();
        long delta = time.value;
        this.lastDelta = delta;
        this.time += delta;
        this.lastUpdated = currentTime;
        return this;
    }
    //Controls
    final public synchronized Stopwatch resume(){
        if(this.isRunning) return this;
        this.isRunning = true;
        this.lastUpdated = System.currentTimeMillis();
        if(this.time == 0) this.onStart();
        else this.onResume();
        return this;
    }
    final public synchronized Stopwatch pause(){
        if(!this.isRunning) return this;
        long time = System.currentTimeMillis();
        this.time += time - this.lastUpdated;
        this.isRunning = false;
        this.onPause();
        return this;
    }
    final public synchronized Stopwatch stop(){
        this.isRunning = false;
        this.time = 0;
        this.onStop();
        return this;
    }

    //Events
    protected void onResume(){}
    protected void onStart(){}
    protected void onPause(){}
    protected void onStop(){}

    //Questions about controls
    final public boolean isRunning(){
        return this.isRunning;
    }
    final public boolean isPaused(){
        return !this.isRunning && this.time != 0;
    }
    final public boolean isStopped(){
        return !this.isRunning && this.time == 0;
    }

    final public TimeMeasurement getTime(){
        return new TimeMeasurement(this.update().time);
    }
    final public long getTimeValue(){
        return this.update().time;
    }
    final public TimeMeasurement getDelta(){
        return new TimeMeasurement(this.lastDelta);
    }
    final public long getDeltaValue(){
        return this.lastDelta;
    }

    final public Stopwatch copy(){
        Stopwatch sw = new Stopwatch();
        sw.time = this.time;
        sw.lastUpdated = this.lastUpdated;
        sw.lastDelta = this.lastDelta;
        sw.isRunning = this.isRunning;
        if(Stopwatch.instances.contains(this)) Stopwatch.instances.add(this);
        return sw;
    }
    final public Stopwatch copy(boolean includeInInstanceList){
        Stopwatch sw = new Stopwatch();
        sw.time = this.time;
        sw.lastUpdated = this.lastUpdated;
        sw.lastDelta = this.lastDelta;
        sw.isRunning = this.isRunning;
        if(includeInInstanceList) Stopwatch.instances.add(this);
        return sw;
    }
}

TimeMeasurement.java

final public class TimeMeasurement implements Comparable<TimeMeasurement>{
    final static public TimeMeasurement ZERO = new TimeMeasurement(0);
    final static public TimeMeasurement SECOND = new TimeMeasurement(1000);
    final static public TimeMeasurement MINUTE = new TimeMeasurement(60000);
    final static public TimeMeasurement HOUR = new TimeMeasurement(3600000);
    final static public TimeMeasurement DAY = new TimeMeasurement(86400000);

    final public long value;

    public TimeMeasurement(long value){
        this.value = value;
    }

    public long getMiliseconds(){
        return this.value % 1000;
    }
    public long getSeconds(){
        return this.value / TimeMeasurement.SECOND.value % 60;
    }
    public long getMinutes(){
        return this.value / TimeMeasurement.MINUTE.value % 60;
    }
    public long getHours(){
        return this.value / TimeMeasurement.HOUR.value % 60;
    }
    public long getDays(){
        return this.value / TimeMeasurement.DAY.value;
    }
    public long getTotalMiliseconds(){
        return this.value;
    }
    public long getTotalSeconds(){
        return this.value / TimeMeasurement.SECOND.value;
    }
    public long getTotalMinutes(){
        return this.value / TimeMeasurement.MINUTE.value;
    }
    public long getTotalHours(){
        return this.value / TimeMeasurement.HOUR.value;
    }
    public long getTotalDays(){
        return this.value / TimeMeasurement.DAY.value;
    }

    public TimeMeasurement subtract(TimeMeasurement time){
        if(time == null) throw new NullPointerException();
        return new TimeMeasurement(this.value - time.value);
    }
    public TimeMeasurement subtract(long time){
        return new TimeMeasurement(this.value - time);
    }
    public TimeMeasurement add(TimeMeasurement time){
        if(time == null) throw new NullPointerException();
        return new TimeMeasurement(this.value + time.value);
    }
    public TimeMeasurement add(long time){
        return new TimeMeasurement(this.value + time);
    }
    public TimeMeasurement difference(TimeMeasurement time){
        if(time == null) throw new NullPointerException();
        return new TimeMeasurement(Math.abs(this.value + time.value));
    }
    public TimeMeasurement difference(long time){
        return new TimeMeasurement(Math.abs(this.value + time));
    }

    public int compareTo(TimeMeasurement time){
        if(time == null) throw new NullPointerException();
        long diff = this.value - time.value;
        return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
    }
    @Override public boolean equals(Object time){
        if(time == null) return false;
        if(!(time instanceof TimeMeasurement)) return false;
        return this.value == ((TimeMeasurement)time).value;
    }
}

Alright, Thanks for the help