GregorianCalendar类:
GregorianCalendar():构造一个日历对象,用来表示默认地区,默认时区的当前时间
GregorianCalendar(int year, int month, int day)
GregorianCalendar(int year, int month, int day, int hour, int minutes, int seconds)
int get(int field)
void set(int field, int value)
void set(int year, int month, int day)
void set(int year, int month, int day, int hour, int minutes, int seconds)
void add(int field, int amount)
int getFirstDayOfWeek()
void setTime(Date time)
Date getTime()
----------------
DateFormatSymbols类
获取当前地区的星期几或月份的名称
String[] getShortWeekdays()
Stirng[] getShortMonths()
String[] getWeekdays()
String[] getMonths()
1 import java.text.DateFormatSymbols;
2 import java.util.Calendar;
3 import java.util.GregorianCalendar;
4
5
6 public class CalendarTest {
7
8 /**
9 * @param args
10 */
11
12 /*
13 * GregorianCalendar类
14 */
15 public static void main(String[] args) {
16 // TODO Auto-generated method stub0
17
18 //construct d as current date
19 GregorianCalendar d = new GregorianCalendar();
20
21 int today = d.get(Calendar.DAY_OF_MONTH);
22 int month = d.get(Calendar.MONTH);
23
24 //set d to start date of the month
25 d.set(Calendar.DAY_OF_MONTH, 1);
26
27 int weekday = d.get(Calendar.DAY_OF_WEEK);
28
29 //get first day of week (Sunday in the U.S.)
30 int firstDayOfWeek = d.getFirstDayOfWeek();
31
32 //determine the required indentation for the first line
33 int indent = 0;
34 while(weekday != firstDayOfWeek)
35 {
36 indent++;
37 d.add(Calendar.DAY_OF_MONTH,-1);
38 weekday = d.get(Calendar.DAY_OF_WEEK);
39 }
40
41 //print weekday names;
42 String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
43 do
44 {
45 System.out.println(weekdayNames[weekday]);
46 d.add(Calendar.DAY_OF_MONTH,1);
47 weekday = d.get(Calendar.DAY_OF_WEEK);
48 }while(weekday != firstDayOfWeek);
49 System.out.println();
50 for(int i =1;i<indent;i++)
51 {
52 System.out.print(" ");
53 }
54 d.set(Calendar.DAY_OF_MONTH, 1);
55 do{
56 //print day
57 int day = d.get(Calendar.DAY_OF_MONTH);
58 System.out.print(day);
59
60 //mark current day with *
61 if(day == today) System.out.println("*");
62 else System.out.println(" ");
63
64 //advance d to the next day
65 d.add(Calendar.DAY_OF_WEEK,1);
66
67 //start a new line at the start of the week
68 if(weekday == firstDayOfWeek) System.out.println();
69 }while (d.get(Calendar.MONTH) == month);
70 //the loop exits when d is day 1 of the next month
71
72 //print final end of line if necessary
73 if(weekday != firstDayOfWeek) System.out.println();
74
75 }
76
77 }