calendar模塊中month的作用?
作用解析與事例
package pack.java.calendar.demo;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class CalendarDemo {
private static SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public static void main(String[] args) {
//獲取calendar實例;
Calendar calendar = Calendar.getInstance();
//判斷calendar是不是GregorianCalendar類的實例;
if(calendar instanceof GregorianCalendar){
System.out.println("屬于GregorianCalendar類的實例!");
}
//從calendar對象中獲得date對象,當前時間;
Date dates = calendar.getTime();
//格式化時間;
String date_str= date_format.format(dates);
System.out.println(date_str);
//設置月份05;代表日歷的月份6月,因為月份從0開始。
calendar.set(Calendar.MONTH, 05);
int months = calendar.get(Calendar.MONTH);
System.out.println(months); //輸出05;
//設置日期為2011-07-24 09:59:50
calendar.set(2011, 06, 24, 9, 59, 50);
String getDate = date_format.format(calendar.getTime());
System.out.println(getDate); //輸出2011-07-24 09:59:50;
//比較日前大小;
if(new Date().getTime() > calendar.getTimeInMillis()){
System.out.println("當前日期在后!");
}else{
System.out.println("當前日期在前!");
}
//設置當前時間為:2011-07-24 11:06:00
calendar.setTime(new Date());
int year = calendar.get(Calendar.YEAR); //獲取年;
int month = calendar.get(Calendar.MONTH); //獲取月;
int date = calendar.get(Calendar.DATE); //獲取天;
int hour = calendar.get(Calendar.HOUR); //獲取小時;
int minute = calendar.get(Calendar.MINUTE); //獲取分鐘;
int second = calendar.get(Calendar.SECOND); //獲取秒鐘;
int hour_of_day = calendar.get(Calendar.HOUR_OF_DAY); //第幾個小時,
int day_of_month = calendar.get(Calendar.DAY_OF_MONTH); //這天,在一個月內(nèi)是第幾天.
int day_of_week = calendar.get(Calendar.DAY_OF_WEEK); //這天,在一周內(nèi),是第幾天.
int day_of_year = calendar.get(Calendar.DAY_OF_YEAR); //這天,在一年內(nèi),是第幾天。
int week_of_year = calendar.get(Calendar.WEEK_OF_YEAR); //這周,在一年內(nèi)是第幾周;
int week_of_month = calendar.get(Calendar.WEEK_OF_MONTH);//這周,在這個月是第幾周;以以星為標準;
int zone_offset = calendar.get(Calendar.ZONE_OFFSET); //獲取時區(qū);
int day_of_week_in_month = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH); //某月中第幾周,按這個月1號算,1號起就是第1周,8號起就是第2周。以月份天數(shù)為標準
int r = calendar.get(Calendar.AM_PM);
if(r==calendar.AM){
System.out.println("現(xiàn)在是上午");
}
if(r==calendar.PM){
System.out.println("現(xiàn)在是下午");
}
System.out.println("==================================================");
System.out.println(year);
System.out.println(month);
System.out.println(date);
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
System.out.println(hour_of_day);
System.out.println(day_of_month);
System.out.println(day_of_week);
System.out.println(day_of_year);
System.out.println(week_of_year);
System.out.println(week_of_month);
System.out.println(zone_offset);
System.out.println(day_of_week_in_month);
}
}