Java and time with Processing

Instead of using the Processing millis() function or the Java Timer class, we can also make use of the relatively new Instant and Duration classes in Java 8. Here is one simple example for demonstration.

The program uses 2 Instant variables: start, end. It computes the time duration between them using the Duration.between() function.

import java.time.Instant;
import java.time.Duration;
Instant start;
PFont font;
void setup() {
  size(640, 480);
  start =;
  font = loadFont("AmericanTypewriter-24.vlw");
  textFont(font, 24);
void draw() {
  Instant end =;
  Duration elapsed = Duration.between(start, end);
  long ns = elapsed.toNanos();
  text(Long.toString(ns), 230, 230);