Time in Miranda
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 instance 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 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 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 and times, similar to the original example. Here’s a breakdown of the main differences and similarities:
Java uses the
java.time
package for date and time operations, which is similar in functionality to Go’stime
package.Instead of
time.Now()
, Java usesInstant.now()
to get the current time.To create a specific date and time, Java uses
ZonedDateTime.of()
instead oftime.Date()
.Java’s
ZonedDateTime
class provides methods likegetYear()
,getMonth()
, etc., to extract components of the date and time.For comparisons, Java uses methods like
isBefore()
,isAfter()
, andequals()
onInstant
objects.Java’s
Duration
class is used to represent time intervals, similar to Go’s duration.To add or subtract durations, Java uses the
plus()
andminus()
methods onZonedDateTime
objects.
When you run this program, it will output similar information to the original example, showing the current time, a specific time in the past, various components of that time, comparisons between times, and calculations with durations.
Remember to compile and run the Java program using:
$ javac TimeExample.java
$ java TimeExample
This will display the output, demonstrating the various time and date operations in Java.