Calendar calendar=Calendar.getInstance();//創(chuàng)建日期類
calendar.setTime(date);//獲取時(shí)間
int sumday=calendar.getActualMaximum(Calendar.DATE);//獲取當(dāng)月最大的天數(shù)
public static void main(String[] args)
{
// 字符串轉(zhuǎn)換日期格式
DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
接收傳入?yún)?shù)
String strDate = args[1];
得到日期格式對(duì)象
Date date = fmtDateTime.parse(strDate);
完整顯示今天日期時(shí)間
String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date());
System.out.println(str);
創(chuàng)建 Calendar 對(duì)象
Calendar calendar = Calendar.getInstance();
try
{
// 對(duì) calendar 設(shè)置時(shí)間的方法
// 設(shè)置傳入的時(shí)間格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d H:m:s");
// 指定一個(gè)日期
Date date = dateFormat.parse("2013-6-1 13:24:16");
// 對(duì) calendar 設(shè)置為 date 所定的日期
calendar.setTime(date);
// 按特定格式顯示剛設(shè)置的時(shí)間
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
System.out.println(str);
}
catch (ParseException e)
{
e.printStackTrace();
}
// 或者另一種設(shè)置 calendar 方式
// 分別爲(wèi) year, month, date, hourOfDay, minute, second
calendar = Calendar.getInstance();
calendar.set(2013, 1, 2, 17, 35, 44);
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
System.out.println(str);
// Calendar 取得當(dāng)前時(shí)間的方法
// 初始化 (重置) Calendar 對(duì)象
calendar = Calendar.getInstance();
// 或者用 Date 來初始化 Calendar 對(duì)象
calendar.setTime(new Date());
// setTime 類似上面一行
// Date date = new Date();
// calendar.setTime(date);
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
System.out.println(str);
// 顯示年份
int year = calendar.get(Calendar.YEAR);
System.out.println("year is = " + String.valueOf(year));
// 顯示月份 (從0開始, 實(shí)際顯示要加一)
int month = calendar.get(Calendar.MONTH);
System.out.println("nth is = " + (month + 1));
// 本周幾
int week = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("week is = " + week);
// 今年的第 N 天
int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);
// 本月第 N 天
int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));
// 3小時(shí)以后
calendar.add(Calendar.HOUR_OF_DAY, 3);
int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);
System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);
// 當(dāng)前分鐘數(shù)
int MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE = " + MINUTE);
// 15 分鐘以后
calendar.add(Calendar.MINUTE, 15);
MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE + 15 = " + MINUTE);
// 30分鐘前
calendar.add(Calendar.MINUTE, -30);
MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE - 30 = " + MINUTE);
// 格式化顯示
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
System.out.println(str);
// 重置 Calendar 顯示當(dāng)前時(shí)間
calendar.setTime(new Date());
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
System.out.println(str);
// 創(chuàng)建一個(gè) Calendar 用于比較時(shí)間
Calendar calendarNew = Calendar.getInstance();
// 設(shè)定為 5 小時(shí)以前察绷,后者大恬砂,顯示 -1
calendarNew.add(Calendar.HOUR, -5);
System.out.println("時(shí)間比較:" + calendarNew.compareTo(calendar));
// 設(shè)定7小時(shí)以后蚜点,前者大搞动,顯示 1
calendarNew.add(Calendar.HOUR, +7);
System.out.println("時(shí)間比較:" + calendarNew.compareTo(calendar));
// 退回 2 小時(shí)轧抗,時(shí)間相同,顯示 0
calendarNew.add(Calendar.HOUR, -2);
System.out.println("時(shí)間比較:" + calendarNew.compareTo(calendar));
}
}