fork download
  1. import java.time.*;
  2.  
  3. class Main {
  4. public static void main(String[] args) {
  5. // A sample epoch
  6. long epoch = 1693832142L;
  7.  
  8. // Note: change the timezone as per your requirement
  9. ZoneId userZone = ZoneId.of("America/New_York");
  10.  
  11. // ZonedDateTime at the given epoch
  12. ZonedDateTime zdtGiven = Instant.ofEpochSecond(epoch).atZone(userZone);
  13.  
  14. // LocalDate at the given epoch
  15. LocalDate localDateGiven = zdtGiven.toLocalDate();
  16.  
  17. // Today
  18. LocalDate today = LocalDate.now(userZone);
  19.  
  20. // Get start of today
  21. ZonedDateTime startOfToday = today.atStartOfDay(userZone);
  22.  
  23. // Get start of the next day
  24. ZonedDateTime startOfNextDay = today.plusDays(1).atStartOfDay(userZone);
  25.  
  26. // Current ZonedDateTime
  27. ZonedDateTime zdtNow = ZonedDateTime.now(userZone);
  28.  
  29. // Anything happening before a given time e.g. 10 am today
  30. if (zdtGiven.isAfter(startOfToday) && zdtGiven.isBefore(zdtNow.with(LocalTime.of(10, 0))))
  31. // Do this e.g.
  32. System.out.println("Before 10 am today");
  33.  
  34. // Alternatively
  35. if (localDateGiven.isEqual(today) && zdtGiven.isBefore(zdtNow.with(LocalTime.of(10, 0))))
  36. // Do this e.g.
  37. System.out.println("Before 10 am today");
  38.  
  39. // Anything happening after a given time e.g. 10 am today
  40. if (zdtGiven.isAfter(zdtNow.with(LocalTime.of(10, 0))) && zdtGiven.isBefore(startOfNextDay))
  41. // Do this e.g.
  42. System.out.println("After 10 am today");
  43.  
  44. // Alternatively
  45. if (zdtGiven.isAfter(zdtNow.with(LocalTime.of(10, 0))) && localDateGiven.isEqual(today))
  46. // Do this e.g.
  47. System.out.println("After 10 am today");
  48.  
  49. // Anything happening today
  50. if (localDateGiven.isEqual(today))
  51. // Do this e.g.
  52. System.out.println("Today");
  53. }
  54. }
Success #stdin #stdout 0.13s 40736KB
stdin
Standard input is empty
stdout
Before 10 am today
Before 10 am today
Today