public static void main(String[] args) throws Exception { String startTime = "2012-12-12 12:45:45"; String endTime = "2012-04-12 12:45:40"; String SYSendTime = "2012-11-12 12:45:40"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startDate = sdf.parse(startTime); Date endDate = sdf.parse(endTime); Calendar start = Calendar.getInstance(); Calendar end = Calendar.getInstance(); start.setTime(startDate); end.setTime(endDate); if(start.before(end)){ System.err.println("開始時間小於結束時間"); }else if(start.after(end)){ System.err.println("開始時間大於結束時間"); }else if(start.equals(end)){ System.err.println("開始時間等於結束時間"); } } //經常使用方法 public static void method(){ Date date = new Date(); Calendar c = Calendar.getInstance(); //setTime():使用給定的Date設置此 Calendar 的時間 c.setTime(date); //獲取Calendar對象 Calendar cm = Calendar.getInstance(); //getTime():獲取當前時間,相似於new Date(); Date d = cm.getTime(); System.err.println("Calendar得到時間:" + d); System.err.println("new Date建立的時間:" + date); //getTimeInMillis():返回此 Calendar 的時間值,以毫秒爲單位。 long dl = c.getTimeInMillis(); long ddate = cm.getTimeInMillis(); System.err.println("毫秒數:" + dl); System.err.println("毫秒數:" + ddate); //setTimeInMillis():用給定的 long 值設置此Calendar的當前時間值。 long sv = 123456; Calendar sc = Calendar.getInstance(); sc.setTimeInMillis(sv); SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String st = ss.format(sc.getTime()); System.err.println(st); //get():返回給定日曆字段的值。 int year = c.get(Calendar.YEAR); System.err.println(year); //set():將給定的日曆字段設置爲給定值 c.set(Calendar.YEAR, 2); int y = c.get(Calendar.YEAR); System.err.println(y);//輸出2 //Calendar比較:before(),after(),equals(),compareTo(). try{ String startTime = "2012-12-12 12:45:39"; String endTime = "2012-12-12 12:45:40"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date startDate = sdf.parse(startTime); Date endDate = sdf.parse(endTime); Calendar start = Calendar.getInstance(); Calendar end = Calendar.getInstance(); start.setTime(startDate); end.setTime(endDate); if(start.before(end)){ System.err.println("開始時間小於結束時間"); }else if(start.after(end)){ System.err.println("開始時間大於結束時間"); }else if(start.equals(end)){ System.err.println("開始時間等於結束時間"); } /* * start < end 返回-1 * start = end 返回0 * start > end 返回1 */ int count = start.compareTo(end); System.err.println(count); //add():爲給定的日曆字段添加或減去指定的時間量 start.add(Calendar.YEAR, -3); System.err.println("原來的時間:" + startTime); System.err.println("add後的時間:" + sdf.format(start.getTime())); //toString():轉換爲字符串 System.err.println(start.toString()); }catch(Exception e){} } //經常使用屬性 public static void param(){ Date date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); //Calendar.YEAR:日期中的年 int year = c.get(Calendar.YEAR); //Calendar.MONTH:日期中的月,須要加1 int mounth = c.get(Calendar.MONTH) + 1; //Calendar.DATE:日期中的日 int day = c.get(Calendar.DATE); //Calendar.HOUR:日期中的小時(12小時制) int hour = c.get(Calendar.HOUR); //Calendar.HOUR_OF_DAY:24小時制 int HOUR_OF_DAY = c.get(Calendar.HOUR_OF_DAY); //Calendar.MINUTE:日期中的分鐘 int minute = c.get(Calendar.MINUTE); //Calendar.SECOND:日期中的秒 int second = c.get(Calendar.SECOND); System.err.println(year + "-" + mounth + "-" + day + " " + hour + ":" + minute + ":" + second); //Calendar.WEEK_OF_YEAR:當前年中星期數 int WEEK_OF_YEAR = c.get(Calendar.WEEK_OF_YEAR); //Calendar.WEEK_OF_MONTH:當前月中星期數 int WEEK_OF_MONTH = c.get(Calendar.WEEK_OF_MONTH); //Calendar.DAY_OF_YEAR:當前年中的第幾天 int DAY_OF_YEAR = c.get(Calendar.DAY_OF_YEAR); //Calendar.DAY_OF_MONTH:當前月中的第幾天 int DAY_OF_MONTH = c.get(Calendar.DAY_OF_MONTH); //Calendar.DAY_OF_WEEK:當前星期的第幾天(星期天表示第一天,星期六表示第七天) int DAY_OF_WEEK = c.get(Calendar.DAY_OF_WEEK); //Calendar.DAY_OF_WEEK_IN_MONTH:當前月中的第幾個星期 int DAY_OF_WEEK_IN_MONTH = c.get(Calendar.DAY_OF_WEEK_IN_MONTH); try{ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date ampm = format.parse("2012-12-15 21:59:59"); Calendar cc = Calendar.getInstance(); cc.setTime(ampm); //AM_PM:HOUR 是在中午以前仍是在中午以後,在中午12點以前返回0,在中午12點(包括12點)以後返回1 int AM_PM = cc.get(Calendar.AM_PM); }catch(Exception e){} } }