1.Calendar 轉化 Stringjava
//獲取當前時間的具體狀況,如年,月,日,week,date,分,秒等
Calendar calendat = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime());
2.String 轉化Calendar
String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
3.Date 轉化String
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(new Date());
4.String 轉化Date
String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date birthday = sdf.parse(str);
5.Date 轉化Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date());
6.Calendar轉化Date
Calendar calendar = Calendar.getInstance();
java.util.Date date =calendar.getTime();
下面有個例子,插入oralce表類型爲DATE的數據
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
String date = sdf.format(new Date());
-----------------------------------------
String sql = "Insert into SM_SEND (*****************) " +
"values (*******************************
"',1," + "to_date('" +date+ "','YYYY-MM-DD HH24:MI:SS')" +
"," + "to_date('" +date+ "','YYYY-MM-DD HH24:MI:SS')" +
",0,null,null,null,null,null,null,null)";
注:date轉calendar時使用calendar.setTime();
有時當setTime的參數格式問題會致使轉換異常
實例 1
/** * 獲取上月十五日凌晨時間 * @author: WY * @rise: */
public static String previousMonth15() throws ParseException{ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)-1); calendar.set(Calendar.DAY_OF_MONTH,15); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MINUTE,0); Date strDateTo = calendar.getTime(); SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(strDateTo); }