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.time.temporal.WeekFields;
  8. import java.util.Locale;
  9.  
  10. public class Main {
  11. public static void main(String[] args) {
  12. // A sample epoch millisecond
  13. long millis = 1737198329820L;
  14.  
  15. // Convert epoch millisecond to Instant
  16. Instant instant = Instant.ofEpochMilli(millis);
  17.  
  18. // Convert the instant to ZonedDateTime of the applicable ZoneId
  19. // A sample ZoneId
  20. ZoneId zoneId = ZoneId.of("America/New_York");
  21. ZonedDateTime zdt = instant.atZone(zoneId);
  22. System.out.println("Given date-time: " + zdt);
  23.  
  24. // Get start of the current week
  25. ZonedDateTime zdtStartOfTheCurrentWeekUs = getStartOfTheCurrentWeek(zdt, Locale.US);
  26. System.out.println("Start of the current week: " + zdtStartOfTheCurrentWeekUs);
  27.  
  28. // Get the start of the next week
  29. ZonedDateTime zdtStartOfTheNextWeekUs = zdtStartOfTheCurrentWeekUs.plusWeeks(1);
  30. System.out.println("Start of the next week: " + zdtStartOfTheNextWeekUs);
  31.  
  32. // Check if your epoch millisecond falls within the week
  33. if (!zdt.isBefore(zdtStartOfTheCurrentWeekUs)
  34. && zdt.isBefore(zdtStartOfTheNextWeekUs)) {
  35. System.out.println("The given date-time is within the current week");
  36. } else {
  37. System.out.println("The given date-time is outside the current week");
  38. }
  39. }
  40.  
  41. static ZonedDateTime getStartOfTheCurrentWeek(ZonedDateTime zdt, Locale locale) {
  42. ZoneId zoneId = zdt.getZone();
  43. LocalDate date = zdt.toLocalDate();
  44. DayOfWeek firstDayOfWeek = date.with(WeekFields.of(locale).dayOfWeek(), 1)
  45. .getDayOfWeek();
  46. System.out.printf("1st day of week in %s: %s%n", zoneId, firstDayOfWeek);
  47. return date.atStartOfDay(zoneId)
  48. .with(TemporalAdjusters.previousOrSame(firstDayOfWeek));
  49. }
  50. }
Success #stdin #stdout 0.24s 62132KB
stdin
Standard input is empty
stdout
Given date-time: 2025-01-18T06:05:29.820-05:00[America/New_York]
1st day of week in America/New_York: SUNDAY
Start of the current week: 2025-01-12T00:00-05:00[America/New_York]
Start of the next week: 2025-01-19T00:00-05:00[America/New_York]
The given date-time is within the current week