码迷,mamicode.com
首页 > 其他好文 > 详细

GregorianCalendar类

时间:2015-11-15 14:48:27      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

 

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 }

 

GregorianCalendar类

标签:

原文地址:http://www.cnblogs.com/linst/p/4966533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!