fork download
  1. import java.time.ZoneId;
  2. import java.time.ZonedDateTime;
  3. import java.time.temporal.ChronoUnit;
  4.  
  5. class Main {
  6. public static void main(String[] args) {
  7. ZonedDateTime now = ZonedDateTime.now(ZoneId.of("America/New_York"));
  8. ZonedDateTime then = now.withHour(14).truncatedTo(ChronoUnit.HOURS);
  9. System.out.println(now);
  10. System.out.println(then);
  11.  
  12. // Compare
  13. if (now.isBefore(then))
  14. System.out.println(now + " is before " + then);
  15. else if (now.isEqual(then))
  16. System.out.println(now + " is equal to " + then);
  17. else
  18. System.out.println(now + " is after " + then);
  19.  
  20. // Another ways to compare
  21. if (!now.isAfter(then))
  22. System.out.println(now + " is either before or at " + then);
  23. }
  24. }
Success #stdin #stdout 0.18s 56412KB
stdin
Standard input is empty
stdout
2023-01-31T11:48:40.191880-05:00[America/New_York]
2023-01-31T14:00-05:00[America/New_York]
2023-01-31T11:48:40.191880-05:00[America/New_York] is before 2023-01-31T14:00-05:00[America/New_York]
2023-01-31T11:48:40.191880-05:00[America/New_York] is either before or at 2023-01-31T14:00-05:00[America/New_York]