fork download
  1. import java.time.DayOfWeek;
  2. import java.time.ZonedDateTime;
  3. import java.time.temporal.TemporalAdjusters;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. // Test
  8. System.out.println(whichDowOfTheMonth("2021-06-10T13:30:30+03:00"));
  9. System.out.println(whichDowOfTheMonth("2021-06-17T13:30:30+03:00"));
  10. System.out.println(whichDowOfTheMonth("2021-06-24T13:30:30+03:00"));
  11. }
  12.  
  13. static int whichDowOfTheMonth(String strDateTime) {
  14. ZonedDateTime zdt = ZonedDateTime.parse(strDateTime);
  15. DayOfWeek dow = zdt.getDayOfWeek();
  16.  
  17. ZonedDateTime zdtFirstDowOfMonth = zdt.with(TemporalAdjusters.firstInMonth(dow));
  18. ZonedDateTime zdtLastDayOfMonth = zdt.with(TemporalAdjusters.lastDayOfMonth());
  19.  
  20. int count = 1;
  21. for (ZonedDateTime date = zdtFirstDowOfMonth; !date.isAfter(zdtLastDayOfMonth); date = date.plusDays(7)) {
  22. if (date.equals(zdt))
  23. break;
  24. count++;
  25. }
  26. return count;
  27. }
  28. }
Success #stdin #stdout 0.09s 49688KB
stdin
Standard input is empty
stdout
2
3
4