fork download
  1. import java.time.LocalDateTime;
  2. import java.time.temporal.ChronoUnit;
  3. import java.time.temporal.TemporalAdjusters;
  4. import java.time.temporal.WeekFields;
  5. import java.util.Locale;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. // Test
  10. System.out.println(getDayOfWeekValue(LocalDateTime.of(2021, 11, 5, 17, 14, 24)));
  11. }
  12.  
  13. static int getDayOfWeekValue(LocalDateTime input) {
  14. return Math.toIntExact(
  15. ChronoUnit.DAYS.between(
  16. input.with(
  17. TemporalAdjusters.previousOrSame(
  18. WeekFields.of(Locale.US)
  19. .getFirstDayOfWeek())),
  20. input.plusDays(1)));
  21. // Note: One day has been added as ChronoUnit.DAYS.between excludes
  22. // the second parameter while calculating the number of days
  23. }
  24. }
  25.  
Success #stdin #stdout 0.15s 52168KB
stdin
Standard input is empty
stdout
6