關(guān)于Java的calendar類

Calendar cale =Calendar.getInstance();

int year = cale.get(Calendar.YEAR);//2017

int month =cale.get(Calendar.MONTH)+1;//9月

int day =cale.get(Calendar.DATE);//30日

int hour =cale.get(Calendar.HOUR);//3時

int hour1 =cale.get(Calendar.HOUR_OF_DAY);//15時

int minute =cale.get(Calendar.MINUTE);//55分

int second =cale.get(Calendar.SECOND);//7秒

int dow =cale.get(Calendar.DAY_OF_WEEK)-1;//這周的周幾 --6? (默認周日是1)

int dom =cale.get(Calendar.DAY_OF_MONTH);//30號

int doy =cale.get(Calendar.DAY_OF_YEAR);//今年的多少天-273

int dowm =cale.get(Calendar.DAY_OF_WEEK_IN_MONTH);//這個月的第幾周--5

System.out.println(year);

SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd");

/*獲取上一個月第一天*/

cale =Calendar.getInstance();//獲取當前日期

cale.add(Calendar.MONTH, -1);

cale.set(Calendar.DAY_OF_MONTH, 1);//設(shè)置為1,當前日期就是本月第一天

String firstDay1 = formatter.format(cale.getTime());

System.out.println(firstDay1);//2017-08-01

/*獲取上一個月最后一天*/

cale =Calendar.getInstance();

//cale.add(Calendar.MONTH, 1);

cale.set(Calendar.DAY_OF_MONTH, 0);

String lastDay1 =formatter.format(cale.getTime());

System.out.println(lastDay1);//2017-08-31

/*獲取本月第一天*/

cale =Calendar.getInstance();//獲取當前日期

cale.add(Calendar.MONTH, 0);

cale.set(Calendar.DAY_OF_MONTH, 1);//設(shè)置為1,當前日期就是本月第一天

String firstDay = formatter.format(cale.getTime());

System.out.println(firstDay);//2017-09-01

/*獲取本月最后一天*/

cale =Calendar.getInstance();

cale.add(Calendar.MONTH, 1);

cale.set(Calendar.DAY_OF_MONTH, 0);

String lastDay =formatter.format(cale.getTime());

System.out.println(lastDay);//2017-09-30

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容