[Java] 取得某日的星期數

滿神奇的,我預期是 0-6, 沒想到是 1-7

/**
* Field number for get and set indicating the day
* of the week. This field takes values SUNDAY,
* MONDAY, TUESDAY, WEDNESDAY,
* THURSDAY, FRIDAY, and SATURDAY.
*
* @see #SUNDAY
* @see #MONDAY
* @see #TUESDAY
* @see #WEDNESDAY
* @see #THURSDAY
* @see #FRIDAY
* @see #SATURDAY
*/
public final static int DAY_OF_WEEK = 7;


範例:

import java.util.Calendar;
 
class Main {    
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance(); 
        //Java 中月 1 - 12 月,分別對應是 0 - 11
        //設定目前日期為 2000/02/01(29天)
        //cal.set(2000, 1, 1); 
        System.out.println("現在的時間: " + cal.getTime());
        System.out.println("本月共有  : " + cal.getActualMaximum(Calendar.WEEK_OF_MONTH) + "  週");
        int date_Count = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
        //Java 中星期 日、一、二、三、四、五、六,分別對應是 1 - 7
        if ((cal.get(Calendar.DAY_OF_WEEK)) == 1) {
            System.out.println("今天星期天");
        } else if ((cal.get(Calendar.DAY_OF_WEEK)) == 2) {
            System.out.println("今天星期一");
        } else if ((cal.get(Calendar.DAY_OF_WEEK)) == 3) {
            System.out.println("今天星期二");
        } else if ((cal.get(Calendar.DAY_OF_WEEK)) == 4) {
            System.out.println("今天星期三");
        } else if ((cal.get(Calendar.DAY_OF_WEEK)) == 5) {
            System.out.println("今天星期四");
        } else if ((cal.get(Calendar.DAY_OF_WEEK)) == 6) {
            System.out.println("今天星期五");
        } else if ((cal.get(Calendar.DAY_OF_WEEK)) == 7) {
            System.out.println("今天星期六");
        } 
    }
}

 

相關文章

寫留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *