import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Locale;
public class Main {
public static void main
(String[] args
) { // A sample epoch millisecond
long millis = 1737198329820L;
// Convert epoch millisecond to Instant
Instant instant = Instant.ofEpochMilli(millis);
// Convert the instant to ZonedDateTime of the applicable ZoneId
// A sample ZoneId
ZoneId zoneId = ZoneId.of("America/New_York");
ZonedDateTime zdt = instant.atZone(zoneId);
System.
out.
println("Given date-time: " + zdt
);
// Get start of the current week
ZonedDateTime zdtStartOfTheCurrentWeekUs
= getStartOfTheCurrentWeek
(zdt,
Locale.
US); System.
out.
println("Start of the current week: " + zdtStartOfTheCurrentWeekUs
);
// Get the start of the next week
ZonedDateTime zdtStartOfTheNextWeekUs = zdtStartOfTheCurrentWeekUs.plusWeeks(1);
System.
out.
println("Start of the next week: " + zdtStartOfTheNextWeekUs
);
// Check if your epoch millisecond falls within the week
if (!zdt.isBefore(zdtStartOfTheCurrentWeekUs)
&& zdt.isBefore(zdtStartOfTheNextWeekUs)) {
System.
out.
println("The given date-time is within the current week"); } else {
System.
out.
println("The given date-time is outside the current week"); }
}
static ZonedDateTime getStartOfTheCurrentWeek
(ZonedDateTime zdt,
Locale locale
) { ZoneId zoneId = zdt.getZone();
LocalDate date = zdt.toLocalDate();
DayOfWeek firstDayOfWeek = date.with(WeekFields.of(locale).dayOfWeek(), 1)
.getDayOfWeek();
System.
out.
printf("1st day of week in %s: %s%n", zoneId, firstDayOfWeek
); return date.atStartOfDay(zoneId)
.with(TemporalAdjusters.previousOrSame(firstDayOfWeek));
}
}