1.Calendar 类是一个抽象类,无法直接用 Calendar 类直接创建一个对象要用到getInstance() 方法去获得一个系统默认时区的时间
Calendar a = Calendar.getInstance();
2.常用字段
//当前年 int year = cal.get(Calendar.YEAR);
//当前月,注:Calendar.MONTH从0开始 int month = (cal.get(Calendar.MONTH))+1;
//当前月的第几天:即当前日 int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
//Calendar.DAY_OF_MONTH 和 Calendar.DATE 是等价的 int date = cal.get(Calendar.DATE);
//当前时:HOUR_OF_DAY-24小时制 int hour24 = cal.get(Calendar.HOUR_OF_DAY);
//HOUR-12小时制 int hour12 = cal.get(Calendar.HOUR);
//当前分 int minute = cal.get(Calendar.MINUTE);
//当前秒 int second = cal.get(Calendar.SECOND);
// 星期几 Calendar.DAY_OF_WEEK用数字(1~7)表示(星期日~星期六) int day_of_week = cal.get(Calendar.DAY_OF_WEEK)-1;
//0:上午;1:下午 int ampm = cal.get(Calendar.AM_PM);
//当前年的第几周 int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
//当前月的星期数 int week_of_month = cal.get(Calendar.WEEK_OF_MONTH);
//当前月中的第几个星期 int day_of_week_in_month = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
//当前年的第几天 int day_of_year = cal.get(Calendar.DAY_OF_YEAR);
3.常用构造方法
//构造一个带有默认时区和语言环境的Calendar protected Calendar()
//构造一个带有指定时区和语言环境的Calendar protected Calendar(TimeZone zone,Locale aLocale)
4.常用方法
//获取年月日
public class CalendarDate1 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int week = c.get(Calendar.DAY_OF_WEEK);
int hour = c.get(Calendar.DAY_OF_HOUR);
int min = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
String str = year + "年" + (month + 1) + "月" + day + "日" + "," + changeWeek(week) + "," + hour + ":" + min + ":" + second;
System.out.println(str);
}
private static String changeWeek(int week) {
String str = "";
switch (week) {
cass 1:
str = "周日";
break;
cass 2:
str = "周一";
break;
cass 3:
str = "周二";
break;
cass 4:
str = "周三";
break;
cass 5:
str = "周四";
break;
cass 6:
str = "周五";
break;
cass 7:
str = "周六";
break;
}
return str;
}
}
5.明天学习内容:SimpleDateFormat类的使用