fork download
  1. import java.time.DayOfWeek;
  2. import java.time.Instant;
  3. import java.time.LocalDate;
  4. import java.time.ZoneId;
  5. import java.time.ZonedDateTime;
  6. import java.time.temporal.TemporalAdjusters;
  7. import java.util.stream.Stream;
  8.  
  9. public class Main {
  10. public static void main(String[] args) {
  11. // Test
  12. Stream.of(
  13. 1621533017083L,
  14. 1621446617083L,
  15. 1621619417083L,
  16. 1621189684296L,
  17. 1621209600000L,
  18. 1621814400000L
  19. ).forEach(millis -> {
  20. System.out.println(millis);
  21. System.out.println(Instant.ofEpochMilli(millis));
  22. System.out.println("Today: " + isToday(millis, "Europe/London"));
  23. System.out.println("Yesterday: " + isYesterday(millis, "Europe/London"));
  24. System.out.println("In the current week: " + isInTheSameWeek(millis, "Europe/London"));
  25. System.out.println();
  26. });
  27. }
  28.  
  29. static boolean isToday(long epochMillis, String timezone) {
  30. ZoneId zoneId = ZoneId.of(timezone);
  31.  
  32. // The start of the day today at this timezone
  33. ZonedDateTime zdtStartOfDayToday = LocalDate.now().atStartOfDay(zoneId);
  34. long millisStartOfDayToday = zdtStartOfDayToday.toInstant().toEpochMilli();
  35.  
  36. // The start of the next day at this timezone
  37. ZonedDateTime zdtStartOfDayNextDay = LocalDate.now().plusDays(1).atStartOfDay(zoneId);
  38. long millisStartOfDayNextDay = zdtStartOfDayNextDay.toInstant().toEpochMilli();
  39.  
  40. return (epochMillis >= millisStartOfDayToday && epochMillis < millisStartOfDayNextDay);
  41. }
  42.  
  43. static boolean isYesterday(long epochMillis, String timezone) {
  44. ZoneId zoneId = ZoneId.of(timezone);
  45.  
  46. // The start of the day today at this timezone
  47. ZonedDateTime zdtStartOfDayToday = LocalDate.now().atStartOfDay(zoneId);
  48. long millisStartOfDayToday = zdtStartOfDayToday.toInstant().toEpochMilli();
  49.  
  50. // The start of the day yesterday at this timezone
  51. ZonedDateTime zdtStartOfDayYesterday = LocalDate.now().minusDays(1).atStartOfDay(zoneId);
  52. long millisStartOfDayYesterday = zdtStartOfDayYesterday.toInstant().toEpochMilli();
  53.  
  54. return (epochMillis >= millisStartOfDayYesterday && epochMillis < millisStartOfDayToday);
  55. }
  56.  
  57. static boolean isInTheSameWeek(long epochMillis, String timezone) {
  58. ZoneId zoneId = ZoneId.of(timezone);
  59.  
  60. // The start of the day today at this timezone
  61. ZonedDateTime zdtStartOfDayToday = LocalDate.now().atStartOfDay(zoneId);
  62.  
  63. // The start of the week at this timezone
  64. ZonedDateTime zdtStartOfTheWeek = zdtStartOfDayToday.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
  65. long millisStartOfTheWeek = zdtStartOfTheWeek.toInstant().toEpochMilli();
  66.  
  67. // The start of the next week at this timezone
  68. ZonedDateTime zdtStartOfTheNextWeek = zdtStartOfDayToday.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
  69. long millisStartOfTheNextWeek = zdtStartOfTheNextWeek.toInstant().toEpochMilli();
  70.  
  71. return (epochMillis >= millisStartOfTheWeek && epochMillis < millisStartOfTheNextWeek);
  72. }
  73. }
Success #stdin #stdout 0.16s 55412KB
stdin
Standard input is empty
stdout
1621533017083
2021-05-20T17:50:17.083Z
Today: true
Yesterday: false
In the current week: true

1621446617083
2021-05-19T17:50:17.083Z
Today: false
Yesterday: true
In the current week: true

1621619417083
2021-05-21T17:50:17.083Z
Today: false
Yesterday: false
In the current week: true

1621189684296
2021-05-16T18:28:04.296Z
Today: false
Yesterday: false
In the current week: false

1621209600000
2021-05-17T00:00:00Z
Today: false
Yesterday: false
In the current week: true

1621814400000
2021-05-24T00:00:00Z
Today: false
Yesterday: false
In the current week: false