fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.time.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class Ideone
  8. {
  9. public static void main(String[] args) throws Exception {
  10. int year = -1, month = -1;
  11.  
  12. if (args.length == 2) {
  13. year = Integer.parseInt(args[0]);
  14. month = Integer.parseInt(args[1]);
  15. } else {
  16. Scanner sc = new Scanner(System.in);
  17. year = sc.nextInt();
  18. month = sc.nextInt();
  19. }
  20.  
  21. LocalDate d = LocalDate.of(year, month, 1);
  22. int wn = d.getDayOfWeek().getValue() % 7;
  23. int lm = d.lengthOfMonth();
  24.  
  25. System.out.println(year + " 年 " + month + " 月");
  26. System.out.println("日 月 火 水 木 金 土");
  27.  
  28. if (wn > 0) {
  29. for (int i = 0; i < wn; i++)
  30. System.out.print(" ");
  31. }
  32.  
  33. for (int i = 1; i <= lm; i++) {
  34. System.out.printf("%2d", i);
  35. wn++;
  36. if (wn >= 7 || i == lm) {
  37. System.out.println();
  38. wn = 0;
  39. } else {
  40. System.out.print(' ');
  41. }
  42. }
  43. }
  44. }
Success #stdin #stdout 0.27s 34896KB
stdin
2018 5
stdout
2018 年 5 月
日 月 火 水 木 金 土
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31