java 与数据库中的类型的转换

1.将 java.util.Date ,java.sql.Date,String,转换为java.sql.Timestamp

//时间格式列表

private static List<SimpleDateFormat> sfList = new LinkedList<SimpleDateFormat>();
static{
sfList.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
sfList.add(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));
sfList.add(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"));
sfList.add(new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"));
sfList.add(new SimpleDateFormat("yy/M/d HH/mm/ss"));
sfList.add(new SimpleDateFormat("yyyy年MM月dd日"));
sfList.add(new SimpleDateFormat("yyyy/MM/dd"));
sfList.add(new SimpleDateFormat("yyyy-MM-dd"));
sfList.add(new SimpleDateFormat("yyyy.MM.dd"));
sfList.add(new SimpleDateFormat("yy/M/d"));
sfList.add(new SimpleDateFormat("MM/dd/yyyy"));
sfList.add(new SimpleDateFormat("dd/MM/yyyy"));
sfList.add(new SimpleDateFormat("d/M/yy"));
sfList.add(new SimpleDateFormat("M/d/yy"));

}

public Timestamp convert(Object obj) throws Exception {

// TODO 自动生成的方法存根

//Timestamp 转为 Timestamp
if(obj instanceof Timestamp) {
Timestamp time =(Timestamp)obj;
return time;
}
// String 转为 Timestamp
else if(obj instanceof String)
{
Timestamp returnTime = null;
java.util.Date time = null;
for(SimpleDateFormat sf : sfList){
try{
time = sf.parse(""+obj);
}catch(Exception ex){}
if(time != null){
break;
}
}
if(time != null){
returnTime = new Timestamp(time.getTime());
}
return returnTime;
}
else if(obj instanceof Date)
{
//将Date 转换为 Timestamp 
Date date =(Date)obj;
//将String 转换为Timestamp
Timestamp time = new Timestamp(date.getTime());
return time;
}else if(obj instanceof java.util.Date)
{
Timestamp time = new Timestamp(((java.util.Date)obj).getTime());
return time;
}
return (Timestamp)obj;

}


2. 将java.sql.Timestamp ,java.util.Date,String 转换为java.sql.Date


//时间格式化列表

private static List<SimpleDateFormat> sfList = new LinkedList<SimpleDateFormat>();
static{
sfList.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
sfList.add(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));
sfList.add(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"));
sfList.add(new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"));
sfList.add(new SimpleDateFormat("yy/M/d HH/mm/ss"));
sfList.add(new SimpleDateFormat("yyyy年MM月dd日"));
sfList.add(new SimpleDateFormat("yyyy/MM/dd"));
sfList.add(new SimpleDateFormat("yyyy-MM-dd"));
sfList.add(new SimpleDateFormat("yyyy.MM.dd"));
sfList.add(new SimpleDateFormat("yy/M/d"));
sfList.add(new SimpleDateFormat("MM/dd/yyyy"));
sfList.add(new SimpleDateFormat("dd/MM/yyyy"));
sfList.add(new SimpleDateFormat("d/M/yy"));
sfList.add(new SimpleDateFormat("M/d/yy"));

}

public Date convert(Object obj) throws Exception {
// TODO 自动生成的方法存根

//Timestamp 转为 Date
if(obj instanceof Timestamp) {
Timestamp now = (Timestamp)obj;
Date sqldate = new Date(now.getTime());
return sqldate;
}
// String 转为 Date
else if(obj instanceof String)
{
Date returnTime = null;
java.util.Date time = null;
for(SimpleDateFormat sf : sfList){
try{
time = sf.parse(""+obj);
}catch(Exception ex){}
if(time != null){
break;
}
}
if(time != null){
returnTime = new Date(time.getTime());
}
return returnTime;
}
//从 Date 转换为 Date
else if(obj instanceof Date)
{
Date date = (Date)obj;
return date;
}
else if(obj instanceof java.util.Date)
{
Date date = new Date(((java.util.Date)obj).getTime());
return date;
}
return (Date)obj;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值