有不少 字段 如 YEAR MONTH DAY DAY_OF_MONTH DAY_OF_YEAR
注意點: 月份從0開始 星期日是一個星期的第一天java
Calendar c2 = Calendar.getInstance(); // getInstance() 返回一個子類對象
System.out.println(c1.get(Calendar.YEAR)); //獲取年post
import java.util.Calendar; public class demon5_calendar { public static void main(String[] args) { //demon1(); Calendar c2 = Calendar.getInstance(); //c2.set(Calendar.YEAR, 2000); c2.set(1992, 5, 24); c2.add(Calendar.YEAR, 1); System.out.println(c2.get(Calendar.YEAR) + "年" + getNum((c2.get(Calendar.MONTH)+1)) + "月" + getNum(c2.get(Calendar.DAY_OF_MONTH)) + "日" + getWeek(c2.get(Calendar.DAY_OF_WEEK))); } public static void demon1() { Calendar c1 = Calendar.getInstance();//父類引用指向子類對象 System.out.println(c1); System.out.println(c1.get(Calendar.YEAR)); System.out.println(c1.get(Calendar.MONTH)); System.out.println(c1.get(Calendar.DAY_OF_MONTH)); System.out.println(c1.get(Calendar.YEAR) + "年" + (c1.get(Calendar.MONTH)+1) + "月" + getNum(c1.get(Calendar.DAY_OF_MONTH)) + "日" + getWeek(c1.get(Calendar.DAY_OF_WEEK))); } public static String getWeek(int week) { String[] arr = {"","星期日","星期一","星期二","星期三","星期四", "星期五","星期六",}; return arr[week]; } public static String getNum(int n){ return n<9?("0"+n):("" + n); } }
import java.util.Calendar; import java.util.Scanner; public class test3 { //判斷年份是平年仍是閏年 public static void main(String[] args) { System.out.println("請輸入年份:"); Scanner s1 = new Scanner(System.in); int year = s1.nextInt(); getYear(year); } public static void getYear(int year) { Calendar c1 = Calendar.getInstance(); c1.set(year,2,1); c1.add(Calendar.DAY_OF_MONTH, -1); if (c1.get(Calendar.DAY_OF_MONTH)==29) { System.out.println("閏年"); } else { System.out.println("平年"); } } }