fork download
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. class Test{
  6. public static void main(String args[]){
  7. // 標準のコンストラクタでは、現在の日時が設定される。
  8. printCalendar(cal);
  9.  
  10. // 今から30日前
  11. cal.add(Calendar.DATE, -30);
  12. printCalendar(cal);
  13.  
  14. // ありえない日付を設定するとこうなる。
  15. cal.set(2004,cal.add(Calendar.FEBRUARY, 31);
  16. //cal.set(2004,cal.add(cal.FEBRUARY, 31); としても同じ
  17. printCalendar(cal);
  18. }
  19.  
  20. public static void printCalendar(Calendar cal){
  21. char youbi[] = {' ','日','月','火','水','木','金','土'};
  22. String str = "";
  23.  
  24. str += cal.get(Calendar.YEAR) + "年";
  25. str += (cal.get(Calendar.MONTH)+1) + "月";
  26. str += cal.get(Calendar.DATE) + "日(";
  27.  
  28. str += youbi[cal.get(Calendar.DAY_OF_WEEK)];
  29. str += ") ";
  30. str += cal.get(Calendar.HOUR) + ":";
  31. str += cal.get(Calendar.MINUTE) + ":";
  32. str += cal.get(Calendar.SECOND) + "(";
  33. str += cal.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM" ;
  34. str += ") です。";
  35. System.out.println(str);
  36. }
  37. }
  38.  
  39.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:16: error: ')' expected
        cal.set(2004,cal.add(Calendar.FEBRUARY, 31);
                                                   ^
1 error
stdout
Standard output is empty