将Date转换成String Date和TIMESTAMP

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

 public static String converterDateToString(Date date){
  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  return sdf.format(date);
 }
}

 

public static Date convertTimestampToDate(TIMESTAMP date){
  Date realDate=null;
  try {
   realDate=date.dateValue();
  } catch ( SQLException e) {
   // TODO: handle exception
   e.printStackTrace();
  }
  return realDate;
 }

 

使用实例:

 public Integer addBook(BookInfo book) {
  // TODO Auto-generated method stub
  String sql="insert into bookInfo values(book_seq.nextval,?,?,?,to_date(?,'yyyy-MM-dd'),?)";
  Object[] parameters={
    book.getBookName(),
    book.getBookAuthor(),
    book.getBookPrice(),
    DateUtil.converterDateToString(book.getBookPublishDate()),
    book.getType().getTypeId(),
    
  };
  return SQLUtil.executeUpdate(sql, parameters);
 }

 

 

for (Map<String,Object> map : list) {
    TIMESTAMP date=(TIMESTAMP) map.get("BOOKPUBLISHDATE");
    Date realDate=DateUtil.convertTimestampToDate(date);
    map.put("BOOKPUBLISHDATE", realDate);
   }

 

posted on 2016-07-25 17:05  yang32  阅读(268)  评论(0编辑  收藏  举报

导航