fork download
  1. import java.time.LocalDate;
  2. import java.time.ZoneId;
  3. import java.time.ZonedDateTime;
  4. import java.time.temporal.ChronoUnit;
  5.  
  6. class Main {
  7. public static void main(String[] args) {
  8. // A sample hour indicated by the user
  9. int hour = 16;
  10.  
  11. // Replace the ZoneId with the applicable one
  12. ZoneId zone = ZoneId.of("America/New_York");
  13. ZonedDateTime then = LocalDate.now(zone)
  14. .atStartOfDay(zone)
  15. .withHour(hour);
  16.  
  17. ZonedDateTime now = ZonedDateTime.now(zone);
  18. System.out.println(now);
  19.  
  20. if (then.isAfter(now))
  21. then = then.minusDays(1).withHour(hour);
  22.  
  23. long seconds = ChronoUnit.SECONDS.between(then, now);
  24. System.out.println(seconds);
  25. }
  26. }
Success #stdin #stdout 0.13s 51844KB
stdin
Standard input is empty
stdout
2023-01-08T15:17:43.887265-05:00[America/New_York]
83863