2017-02-09 15:33:13
1 Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); //獲取東八區時間 2 3 //獲取年 4 int year = c.get(Calendar.YEAR); 5 //獲取月份,0表示1月份 6 int month = c.get(Calendar.MONTH) + 1; 7 //獲取當前天數 8 int day = c.get(Calendar.DAY_OF_MONTH); 9 //獲取本月最小天數 10 int first = c.getActualMinimum(Calendar.DAY_OF_MONTH); 11 //獲取本月最大天數 12 int last = c.getActualMaximum(Calendar.DAY_OF_MONTH); 13 //獲取當前小時 14 int time = c.get(Calendar.HOUR_OF_DAY); 15 //獲取當前分鍾 16 int min = c.get(Calendar.MINUTE); 17 //獲取當前秒 18 int sec = c.get(Calendar.SECOND); 19 20 SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 21 22 String curDate = s.format(c.getTime()); //當前日期 23 System.out.println("當前時間:" + year + "-" + month + "-" + day + " " + time + ":" + min + ":" + sec); 24 System.out.println("第一天和最后天:" + first +"," + last); 25 System.out.println("當前日期:" + curDate);
輸出結果:
當前時間:2017-2-9 15:42:29
第一天和最后天:1,28
當前日期:2017-02-09 15:42:29