Time in Logo

Java offers extensive support for dates and times through the 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.
        LocalDateTime now = LocalDateTime.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.isBefore(now.atZone(ZoneId.of("UTC"))));
        System.out.println(then.isAfter(now.atZone(ZoneId.of("UTC"))));
        System.out.println(then.isEqual(now.atZone(ZoneId.of("UTC"))));

        // The Duration class represents the interval between two times.
        Duration diff = Duration.between(then, now.atZone(ZoneId.of("UTC")));
        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 and execute it using the javac and java commands:

$ javac TimeExample.java
$ java TimeExample
2023-05-24T12:34:56.789012345
2009-11-17T20:34:58.651387237Z[UTC]
2009
NOVEMBER
17
20
34
58
651387237
UTC
TUESDAY
true
false
false
PT118258H0M58.137625108S
118258
7095480
25543858
25543858137625108
2023-05-24T18:35:56.789012345Z
1996-05-13T22:33:59.513762129Z

This Java code demonstrates various operations with dates and times, similar to the original example. It uses classes from the java.time package, which provides a comprehensive date-time model for Java.

The LocalDateTime class is used for date-time without a time zone, while ZonedDateTime is used for a date-time with a time zone. The Duration class represents a time-based amount of time.

Methods like isBefore(), isAfter(), and isEqual() are used for comparing times. The plus() and minus() methods are used to add or subtract durations from a date-time.

Note that Java’s date-time API is more explicit about time zones compared to some other languages. When comparing times or calculating durations between times in different time zones, you need to ensure they’re in the same time zone (as done here by converting now to UTC when comparing with then).