fork(1) 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.format.* ;
  9. import java.time.temporal.* ;
  10.  
  11.  
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. class Ideone
  14. {
  15. public static void main (String[] args) throws java.lang.Exception
  16. {
  17. DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( Locale.US ) ;
  18.  
  19. YearMonth ym = YearMonth.of( 2013 , Month.JANUARY ) ;
  20. LocalDate firstOfMonth = ym.atDay( 1 ) ;
  21.  
  22. TemporalAdjuster ta = TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) ;
  23. LocalDate previousOrSameMonday = firstOfMonth.with( ta ) ;
  24.  
  25. LocalDate endOfMonth = ym.atEndOfMonth() ;
  26. LocalDate weekStart = previousOrSameMonday ;
  27. do {
  28. LocalDate weekStop = weekStart.plusDays( 6 ) ;
  29. System.out.println( "Week: " + weekStart.format( f ) + " to " + weekStop.format( f ) ) ;
  30. // Set up the next loop.
  31. weekStart = weekStart.plusWeeks( 1 ) ;
  32. } while ( ! weekStart.isAfter( endOfMonth ) ) ;
  33.  
  34. }
  35. }
Success #stdin #stdout 0.17s 39900KB
stdin
Standard input is empty
stdout
Week: Monday, December 31, 2012 to Sunday, January 6, 2013
Week: Monday, January 7, 2013 to Sunday, January 13, 2013
Week: Monday, January 14, 2013 to Sunday, January 20, 2013
Week: Monday, January 21, 2013 to Sunday, January 27, 2013
Week: Monday, January 28, 2013 to Sunday, February 3, 2013