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’s time package.
Instead of time.Now(), Java uses Instant.now() to get the current time.
To create a specific date and time, Java uses ZonedDateTime.of() instead of time.Date().
Java’s ZonedDateTime class provides methods like getYear(), getMonth(), etc., to extract components of the date and time.
For comparisons, Java uses methods like isBefore(), isAfter(), and equals() on Instant 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() and minus() methods on ZonedDateTime 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:
This will display the output, demonstrating the various time and date operations in Java.