判斷當前日期為星期幾(Calendar類方法的使用)
阿新 • • 發佈:2019-02-04
java.util.Calendar(抽象類):它表示一個“日曆”,可以進行日期運算;它是一個抽象類,不能直接使用。我們可以通過它的一個靜態方法:getInstance()獲取一個物件。
public class Demo { public static void main(String[] args) { Calendar c = Calendar.getInstance(); System.out.println(c); //1.常用方法:get(int field) int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH) + 1; int date = c.get(Calendar.DAY_OF_MONTH); int hour = c.get(Calendar.HOUR); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); int week = c.get(Calendar.DAY_OF_WEEK);//1--7的值,對應:星期日,星期一,星期二,星期三....星期六 System.out.println(year + "年" + month + "月" + date + "日 " + hour + ":" + minute + ":" + second); System.out.println(getWeek(week)); } //匹配 public static String getWeek(int week) { //1.製作表: String[] arr = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; return arr[week - 1]; } }