fork(1) download
  1. import java.util.*;
  2. import static java.lang.System.*;
  3.  
  4. class Koyomi2 {
  5. public static void main(String[] args) {
  6. Scanner scan = new Scanner(in);
  7. Date today = new Date();
  8. Calendar cal = Calendar.getInstance();
  9. cal.setTime(today);
  10. int yearNow = cal.get(Calendar.YEAR);
  11. int monthNow = cal.get(Calendar.MONTH);
  12. int day = cal.get(Calendar.DAY_OF_MONTH);
  13. out.println("今日は" + yearNow + "年" + (monthNow + 1) + "月" + day + "日です。");
  14.  
  15. String yearStr = null;
  16. String monthStr = null;
  17. while(true) {
  18. out.print("何年のカレンダーを見ますか?2000~2040までの数字を入力して下さい。>");
  19. yearStr = scan.next();
  20. if (yearStr.matches("20[0-3][0-9]") || yearStr.matches("2040")) {
  21. break;
  22. }
  23. out.println("正しい入力ではありません。");
  24. }
  25. int year = Integer.parseInt(yearStr);
  26. boolean isUruu = false;
  27. if (year % 400 == 0) {
  28. isUruu = true;
  29. } else if (year % 100 == 0) {
  30. isUruu = false;
  31. } else if (year % 4 == 0) {
  32. isUruu = true;
  33. } else {
  34. isUruu = false;
  35. }
  36.  
  37. while(true) {
  38. out.print("何月のカレンダーを見ますか?1~12までの数字を入力して下さい。>");
  39. monthStr = scan.next();
  40. if (monthStr.matches("[1-9]") || monthStr.matches("[1][0-2]")) {
  41. break;
  42. }
  43. out.println("正しい入力ではありません。");
  44. }
  45. int month = Integer.parseInt(monthStr) - 1;
  46. out.println(" " + year + "年 " + (month + 1) + "月");
  47. out.println("日 月 火 水 木 金 土");
  48.  
  49. Calendar cal2 = Calendar.getInstance();
  50. cal2.set(Calendar.YEAR, year);
  51. cal2.set(Calendar.MONTH, month);
  52. cal2.set(Calendar.DAY_OF_MONTH, 1);
  53. int youbi = cal2.get(Calendar.DAY_OF_WEEK);
  54. boolean isDaigetsu = false;
  55. boolean isShogetsu = false;
  56. if (month == 1 - 1 || month == 3 - 1 || month == 5 - 1 || month == 7 - 1
  57. || month == 8 - 1 || month == 10 - 1 || month == 12 - 1) {
  58. isDaigetsu = true;
  59. }
  60. if (month == 4 - 1 || month == 6 - 1 || month == 9 - 1 || month == 11 - 1) {
  61. isShogetsu = true;
  62. }
  63.  
  64. if (youbi == 1) {
  65. out.print(" 1 2 3 4 5 6 7\n"
  66. + " 8 9 10 11 12 13 14\n"
  67. + "15 16 17 18 19 20 21\n"
  68. + "22 23 24 25 26 27 28");
  69. if (isDaigetsu) {
  70. out.println("\n29 30 31");
  71. }
  72. if (isShogetsu) {
  73. out.println("\n29 30");
  74. }
  75. if (isUruu && month == 2 - 1) {
  76. out.println("\n29");
  77. }
  78. } else if (youbi == 2) {
  79. out.print(" 1 2 3 4 5 6\n"
  80. + " 7 8 9 10 11 12 13\n"
  81. + "14 15 16 17 18 19 20\n"
  82. + "21 22 23 24 25 26 27\n"
  83. + "28");
  84. if (isDaigetsu) {
  85. out.println(" 29 30 31");
  86. }
  87. if (isShogetsu) {
  88. out.println(" 29 30");
  89. }
  90. if (isUruu && month == 2 - 1) {
  91. out.println(" 29");
  92. }
  93. } else if (youbi == 3) {
  94. out.print(" 1 2 3 4 5\n"
  95. + " 6 7 8 9 10 11 12\n"
  96. + "13 14 15 16 17 18 19\n"
  97. + "20 21 22 23 24 25 26\n"
  98. + "27 28");
  99. if (isDaigetsu) {
  100. out.println(" 29 30 31");
  101. }
  102. if (isShogetsu) {
  103. out.println(" 29 30");
  104. }
  105. if (isUruu && month == 2 - 1) {
  106. out.println(" 29");
  107. }
  108. } else if (youbi == 4) {
  109. out.print(" 1 2 3 4\n"
  110. + " 5 6 7 8 9 10 11\n"
  111. + "12 13 14 15 16 17 18\n"
  112. + "19 20 21 22 23 24 25\n"
  113. + "26 27 28");
  114. if (isDaigetsu) {
  115. out.println(" 29 30 31");
  116. }
  117. if (isShogetsu) {
  118. out.println(" 29 30");
  119. }
  120. if (isUruu && month == 2 - 1) {
  121. out.println(" 29");
  122. }
  123. } else if (youbi == 5) {
  124. out.print(" 1 2 3\n"
  125. + " 4 5 6 7 8 9 10\n"
  126. + "11 12 13 14 15 16 17\n"
  127. + "18 19 20 21 22 23 24\n"
  128. + "25 26 27 28");
  129. if (isDaigetsu) {
  130. out.println(" 29 30 31");
  131. }
  132. if (isShogetsu) {
  133. out.println(" 29 30");
  134. }
  135. if (isUruu && month == 2 - 1) {
  136. out.println(" 29");
  137. }
  138. } else if (youbi == 6) {
  139. out.print(" 1 2\n"
  140. + " 3 4 5 6 7 8 9\n"
  141. + "10 11 12 13 14 15 16\n"
  142. + "17 18 19 20 21 22 23\n"
  143. + "24 25 26 27 28");
  144. if (isDaigetsu) {
  145. out.println(" 29 30\n31");
  146. }
  147. if (isShogetsu) {
  148. out.println(" 29 30");
  149. }
  150. if (isUruu && month == 2 - 1) {
  151. out.println(" 29");
  152. }
  153. } else if (youbi == 7) {
  154. out.print(" 1\n"
  155. + " 2 3 4 5 6 7 8\n"
  156. + " 9 10 11 12 13 14 15\n"
  157. + "16 17 18 19 20 21 22\n"
  158. + "23 24 25 26 27 28");
  159. if (isDaigetsu) {
  160. out.println(" 29\n30 31");
  161. }
  162. if (isShogetsu) {
  163. out.println(" 29\n30");
  164. }
  165. if (isUruu && month == 2 - 1) {
  166. out.println("29");
  167. }
  168. } else {}
  169. }
  170. }
Success #stdin #stdout 0.14s 30144KB
stdin
0
-1
a
null
2016
2
stdout
今日は2018年5月26日です。
何年のカレンダーを見ますか?2000~2040までの数字を入力して下さい。>正しい入力ではありません。
何年のカレンダーを見ますか?2000~2040までの数字を入力して下さい。>正しい入力ではありません。
何年のカレンダーを見ますか?2000~2040までの数字を入力して下さい。>正しい入力ではありません。
何年のカレンダーを見ますか?2000~2040までの数字を入力して下さい。>正しい入力ではありません。
何年のカレンダーを見ますか?2000~2040までの数字を入力して下さい。>何月のカレンダーを見ますか?1~12までの数字を入力して下さい。>     2016年 2月
日 月 火 水 木 金 土
    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