fork(3) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.time.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. System.out.println(numberOfWeekendContained(LocalDate.of(2016, 12, 2), LocalDate.of(2016, 12, 24)));
  14. }
  15.  
  16. public static int numberOfWeekendContained(LocalDate start, LocalDate end) {
  17. LocalDate firstMondayAfterStart = start.plusDays(8-start.getDayOfWeek().getValue());
  18. LocalDate lastSundayBeforeEnd = end.minusDays(end.getDayOfWeek().getValue());
  19.  
  20. //int numberOfWeeksBetween = (int)Math.floor((Period.between(firstMondayAfterStart, lastSundayBeforeEnd).getDays() + 1) / 7);
  21. int numberOfWeeksInbetween = Math.floorDiv(Period.between(firstMondayAfterStart, lastSundayBeforeEnd).getDays() + 1, 7);
  22.  
  23. return
  24. Math.max(0, Math.min(2, 7 - start.getDayOfWeek().getValue())) + // weekend days from the start week
  25. Math.max(0, end.getDayOfWeek().getValue() - 5) + // weekend days from the end week
  26. numberOfWeeksInbetween * 2; // weekend days from the weeks inbetween
  27.  
  28. }
  29. }
Success #stdin #stdout 0.09s 711168KB
stdin
Standard input is empty
stdout
7