Time in Squirrel

Java offers extensive support for times and durations; here are some examples.

import java.time.*;
import java.time.temporal.ChronoUnit;

public class TimeExample {
    public static void main(String[] args) {
        // We'll start by getting the current time.
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        // You can build a LocalDateTime by providing the
        // year, month, day, hour, minute, second, and nanosecond.
        // Times are always associated with a time zone.
        ZonedDateTime then = ZonedDateTime.of(2009, 11, 17, 20, 34, 58, 651387237, ZoneId.of("UTC"));
        System.out.println(then);

        // You can extract the various components of the time
        // value as expected.
        System.out.println(then.getYear());
        System.out.println(then.getMonth());
        System.out.println(then.getDayOfMonth());
        System.out.println(then.getHour());
        System.out.println(then.getMinute());
        System.out.println(then.getSecond());
        System.out.println(then.getNano());
        System.out.println(then.getZone());

        // The Monday-Sunday DayOfWeek is also available.
        System.out.println(then.getDayOfWeek());

        // These methods compare two times, testing if the
        // first occurs before, after, or at the same time
        // as the second, respectively.
        System.out.println(then.isBefore(ZonedDateTime.now()));
        System.out.println(then.isAfter(ZonedDateTime.now()));
        System.out.println(then.isEqual(ZonedDateTime.now()));

        // The Duration class represents the interval between two times.
        Duration diff = Duration.between(then, ZonedDateTime.now());
        System.out.println(diff);

        // We can compute the length of the duration in
        // various units.
        System.out.println(diff.toHours());
        System.out.println(diff.toMinutes());
        System.out.println(diff.getSeconds());
        System.out.println(diff.toNanos());

        // You can use plus to advance a time by a given
        // duration, or with a minus to move backwards by a
        // duration.
        System.out.println(then.plus(diff));
        System.out.println(then.minus(diff));
    }
}

To run the program, compile it and use java:

$ javac TimeExample.java
$ java TimeExample
2023-06-09T12:34:56.789012345
2009-11-17T20:34:58.651387237Z[UTC]
2009
NOVEMBER
17
20
34
58
651387237
UTC
TUESDAY
true
false
false
PT118695H15M15.142266763S
118695
7121515
25637315
25637315142266763
2023-06-09T18:50:13.793654Z[UTC]
1996-04-27T22:19:43.509120474Z[UTC]

In Java, we use the java.time package for working with dates, times, and durations. The LocalDateTime class represents a date-time without a time zone, while ZonedDateTime includes time zone information. The Duration class is used to represent a time span.

Java’s time API provides methods similar to those in Go for comparing times, extracting components, and performing arithmetic operations on dates and times.

Next, we’ll look at the related idea of time relative to the Unix epoch.