Time in Minitab

Java provides comprehensive support for dates, times, and durations through its java.time package. 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.
        Instant now = Instant.now();
        System.out.println(now);

        // You can create a ZonedDateTime by providing the year, month, day, etc.
        // Times are always associated with a ZoneId, i.e. 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.toInstant().isBefore(now));
        System.out.println(then.toInstant().isAfter(now));
        System.out.println(then.toInstant().equals(now));

        // The Duration class represents the interval between two instants.
        Duration diff = Duration.between(then.toInstant(), 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.toSeconds());
        System.out.println(diff.toNanos());

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

To run this program, save it as TimeExample.java, compile it with javac TimeExample.java, and then run it with java TimeExample. The output will be similar to this:

2023-05-31T12:34:56.789Z
2009-11-17T20:34:58.651387237Z[UTC]
2009
NOVEMBER
17
20
34
58
651387237
UTC
TUESDAY
true
false
false
PT118258H0M1.137612763S
118258
7095480
425728801
425728801137612763
2023-05-31T22:34:59.789Z
1996-05-06T18:34:57.513774474Z

This Java example demonstrates the usage of classes from the java.time package, which provides a rich API for handling dates, times, and durations. The concepts are similar to the Go example, but the syntax and specific method names differ.

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