Time in Fortress
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 build a time struct 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 Weekday 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.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.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));
}
}
This Java code demonstrates various operations with dates, times, and durations using the java.time
package, which was introduced in Java 8.
Here’s a breakdown of the main differences and similarities:
In Java, we use
Instant
for a point in time andZonedDateTime
for a date and time with a time zone.The
time.Date()
function in Go is replaced byZonedDateTime.of()
in Java.Java’s
Duration
class is similar to Go’s, representing a time span between two instants.Methods like
Before()
,After()
, andEqual()
in Go are replaced byisBefore()
,isAfter()
, andisEqual()
in Java.The
Sub()
method in Go is replaced byDuration.between()
in Java.Java’s
plus()
andminus()
methods are equivalent to Go’sAdd()
method with positive and negative durations.
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 the Go example, showing various time and duration operations.
Remember that Java’s date and time API provides even more functionality than what’s shown here. For more complex date and time manipulations, you might want to explore classes like LocalDate
, LocalTime
, Period
, and others in the java.time
package.