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;
}