關於Date的一些小總結

1 如下用to_date方法
--insert語句進行插入,如插入日期類型的數據呢
insert into student values(7,'哇哈哈','19',to_date('2012-09-12','yyyy-mm-dd '));

2. 數據庫中java.sql.Date類型提取處理時的處理情況:
	//第一種:處理數據庫提取處理的日期:年月日
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	try {
	java.util.Date utilDate = dateFormat.parse(rs.getDate("begindate").toString())
	} catch (ParseException e) {
		e.printStackTrace();
	}
				
	//第二種:直接提取
	String begindate = rs.getDate("begindate").toString();
				
	//第三種:分開提取
	java.util.Date date = rs.getDate("begindate");
	SimpleDateFormat sy = new SimpleDateFormat("yy");
	SimpleDateFormat sm = new SimpleDateFormat("MM");
	SimpleDateFormat ss = new SimpleDateFormat("dd");
	String syear = sy.format(date);
	String smonth = sm.format(date);
	String sday = ss.format(date);
3. 將字符串轉化爲java.sql.Date形式

//格式化時間
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String birthday = "2012-02-03";
			try {
				java.util.Date date = dateFormat.parse(birthday);
				java.sql.Date sqlDate = new java.sql.Date(date.getTime());
				pstmt.setDate(4, sqlDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			int i = pstmt.executeUpdate();


4. 獲得當前日期
		//獲得當前時間
		Calendar calendar = Calendar.getInstance();
		//得到年
		int year = calendar.get(Calendar.YEAR);
		//得到月
		int month = calendar.get(Calendar.MONTH);
		month = month +1;
		//得到日
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		
		//得到時
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		
		//得到分
		int minite = calendar.get(Calendar.MINUTE);
		
		//得到秒
		int second = calendar.get(Calendar.SECOND);
		
		//獲得日期
		String today = ""+year+"-"+month+"-"+day+" "+hour+":"+minite+":"+second;
		
		System.out.println("當前日期:"+today);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章