[Java]日期函數-月有幾天,今天星期幾
Source File Name : MonthDay.java
-----------------------------------------------------------
import java.util.Calendar;
class MonthDay
{
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("2000/02 have " + cal.getActualMaximum(Calendar.DAY_OF_MONTH) + " days.");
//設定目前日期為 2002/02/01(28天)
cal.set(2002, 1, 1);
System.out.println("2002/02 have " + cal.getActualMaximum(Calendar.DAY_OF_MONTH) + " days.");
//設定目前日期為 2006/01/01(31天)
cal.set(2006, 0, 1);
System.out.println("2006/02 have " + cal.getActualMaximum(Calendar.DAY_OF_MONTH) + " days.");
//Java 中星期 日、一、二、...、六,分別對應是 1 - 7
//設定目前日期為 2005/12/25(星期日)
cal.set(2005, 11, 25);
System.out.println("2005/12/25 is " + cal.get(Calendar.DAY_OF_WEEK) + ".");
//設定目前日期為 2005/12/26(星期一)
cal.set(2005, 11, 26);
System.out.println("2005/12/26 is " + cal.get(Calendar.DAY_OF_WEEK) + ".");
//設定目前日期為 2005/12/27(星期二)
cal.set(2005, 11, 27);
System.out.println("2005/12/27 is " + cal.get(Calendar.DAY_OF_WEEK) + ".");
//設定目前日期為 2005/12/28(星期三)
cal.set(2005, 11, 28);
System.out.println("2005/12/28 is " + cal.get(Calendar.DAY_OF_WEEK) + ".");
//設定目前日期為 2005/12/29(星期四)
cal.set(2005, 11, 29);
System.out.println("2005/12/29 is " + cal.get(Calendar.DAY_OF_WEEK) + ".");
//設定目前日期為 2005/12/30(星期五)
cal.set(2005, 11, 30);
System.out.println("2005/12/30 is " + cal.get(Calendar.DAY_OF_WEEK) + ".");
//設定目前日期為 2005/12/31(星期六)
cal.set(2005, 11, 31);
System.out.println("2005/12/31 is " + cal.get(Calendar.DAY_OF_WEEK) + ".");
}
}
-----------------------------------------------------------
Result :
-----------------------------------------------------------
2000/02 have 29 days.
2002/02 have 28 days.
2006/02 have 31 days.
2005/12/25 is 1.
2005/12/26 is 2.
2005/12/27 is 3.
2005/12/28 is 4.
2005/12/29 is 5.
2005/12/30 is 6.
2005/12/31 is 7.
-----------------------------------------------------------
0 Comments:
張貼留言
<< Home