fork 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.text.SimpleDateFormat;
  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. final List<Date> dates = mondaysFirst(1900, 1902);
  14. final SimpleDateFormat sf = new SimpleDateFormat("dd MMM yyyy");
  15. for (Date date: dates) {
  16. System.out.println(sf.format(date));
  17. }
  18. }
  19.  
  20. public static List<Date> mondaysFirst(int firstYear, int lastYear) {
  21. final List<Date> dates = new ArrayList<>();
  22. final Calendar c1 = Calendar.getInstance(Locale.US);
  23. c1.set(firstYear, 0, 1, 0, 0, 0);
  24. final Calendar c2 = Calendar.getInstance(Locale.US);
  25. c2.set(lastYear, 11, 31, 23, 59, 59);
  26.  
  27. while (c1.before(c2)) {
  28. final int dayOfTheWeek = c1.get(Calendar.DAY_OF_WEEK);
  29. // is sunday
  30. if (dayOfTheWeek == 1) {
  31. dates.add(c1.getTime());
  32. }
  33. c1.add(Calendar.MONTH, 1);
  34. }
  35.  
  36. return dates;
  37. }
  38. }
Success #stdin #stdout 0.1s 380928KB
stdin
Standard input is empty
stdout
01 Apr 1900
01 Jul 1900
01 Sep 1901
01 Dec 1901
01 Jun 1902