fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.time.* ;
  8. import java.time.temporal.* ;
  9.  
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16.  
  17. DayOfWeek firstDayOfWeek = DayOfWeek.MONDAY ;
  18.  
  19. ZoneId z = ZoneId.of( "America/Montreal" ) ;
  20. YearMonth ym = YearMonth.now( z ) ;
  21.  
  22. LocalDate monthStart = ym.atDay( 1 ) ;
  23. LocalDate monthStop = ym.atEndOfMonth() ;
  24.  
  25. LocalDate calendarStart = monthStart.with( TemporalAdjusters.previousOrSame( firstDayOfWeek ) ) ;
  26. LocalDate calendarStop = monthStop.with( TemporalAdjusters.next( firstDayOfWeek ) ) ;
  27.  
  28. long weeks = ChronoUnit.WEEKS.between( calendarStart , calendarStop ) ;
  29.  
  30. System.out.println( "firstDayOfWeek: " + firstDayOfWeek ) ;
  31. System.out.println( "ym: " + ym );
  32. System.out.println( "month: " + monthStart + "/" + monthStop );
  33. System.out.println( "calendar: " + calendarStart + "/" + calendarStop );
  34. System.out.println( "weeks: " + weeks ) ;
  35.  
  36. }
  37. }
Success #stdin #stdout 0.17s 4386816KB
stdin
Standard input is empty
stdout
firstDayOfWeek: MONDAY
ym: 2017-09
month: 2017-09-01/2017-09-30
calendar: 2017-08-28/2017-10-02
weeks: 5