当前位置: 技术问答>java相关
java.util.Date类型向java.sql.Date类型转换之间的一个小问题,请大家进来看看
来源: 互联网 发布时间:2015-06-20
本文导语: 大家执行一下下面的程序,就会发现通过Date(timeDate.getTime())这句的转换,会丢失Date的时间段信息,只保留了日期。不知道大家对此有什么看法,如果要避免,请问有无简单一点的处理? 我分多,所以就多给点……呵...
大家执行一下下面的程序,就会发现通过Date(timeDate.getTime())这句的转换,会丢失Date的时间段信息,只保留了日期。不知道大家对此有什么看法,如果要避免,请问有无简单一点的处理?
我分多,所以就多给点……呵呵
import java.text.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
try {
String str = "2002-2-6 12:22:11";
StringToDate std = new StringToDate();
java.sql.Date date1 = std.string2Date(str);
System.out.println(date1.toString());
}
catch(Exception e) {
System.out.println(e);
}
}
}
class StringToDate {
public java.sql.Date string2Date(String dateString)
throws java.lang.Exception {
DateFormat dateFormat;
dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);
dateFormat.setLenient(false);
java.util.Date timeDate = dateFormat.parse(dateString);//util类型
System.out.println(timeDate.toString());
java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());//sql类型
return dateTime;
}
}
我分多,所以就多给点……呵呵
import java.text.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
try {
String str = "2002-2-6 12:22:11";
StringToDate std = new StringToDate();
java.sql.Date date1 = std.string2Date(str);
System.out.println(date1.toString());
}
catch(Exception e) {
System.out.println(e);
}
}
}
class StringToDate {
public java.sql.Date string2Date(String dateString)
throws java.lang.Exception {
DateFormat dateFormat;
dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);
dateFormat.setLenient(false);
java.util.Date timeDate = dateFormat.parse(dateString);//util类型
System.out.println(timeDate.toString());
java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());//sql类型
return dateTime;
}
}
|
java.sql.type:
protected String helper (ResultSet rs, int dataType, int col)
throws SQLException
{
String retVal = null;
Integer intObj;
// ask for data depending on the datatype
switch(dataType) {
case Types.DATE:
java.sql.Date date = rs.getDate(col);
retVal = date.toString();
break;
case Types.TIME:
java.sql.Time time = rs.getTime(col);
retVal = time.toString();
break;
case Types.TIMESTAMP:
java.sql.Timestamp timestamp = rs.getTimestamp(col);
retVal = timestamp.toString();
break;
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
retVal = rs.getString(col);
break;
case Types.NUMERIC:
case Types.DECIMAL:
java.math.BigDecimal numeric = rs.getBigDecimal(col, 10);
retVal = numeric.toString();
break;
case Types.BIT:
boolean bit = rs.getBoolean(col);
Boolean boolObj = new Boolean(bit);
retVal = boolObj.toString();
break;
case Types.TINYINT:
byte tinyint = rs.getByte(col);
intObj = new Integer(tinyint);
retVal = intObj.toString();
break;
case Types.SMALLINT:
short smallint = rs.getShort(col);
intObj = new Integer(smallint);
retVal = intObj.toString();
break;
case Types.INTEGER:
int integer = rs.getInt(col);
intObj = new Integer(integer);
retVal = intObj.toString();
break;
case Types.BIGINT:
long bigint = rs.getLong(col);
Long longObj = new Long(bigint);
retVal = longObj.toString();
break;
case Types.REAL:
float real = rs.getFloat(col);
Float floatObj = new Float(real);
retVal = floatObj.toString();
break;
case Types.FLOAT:
case Types.DOUBLE:
double longreal = rs.getDouble(col);
Double doubleObj = new Double(longreal);
retVal = doubleObj.toString();
break;
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
byte[] binary = rs.getBytes(col);
retVal = new String(binary);
break;
}
return retVal;
}
protected String helper (ResultSet rs, int dataType, int col)
throws SQLException
{
String retVal = null;
Integer intObj;
// ask for data depending on the datatype
switch(dataType) {
case Types.DATE:
java.sql.Date date = rs.getDate(col);
retVal = date.toString();
break;
case Types.TIME:
java.sql.Time time = rs.getTime(col);
retVal = time.toString();
break;
case Types.TIMESTAMP:
java.sql.Timestamp timestamp = rs.getTimestamp(col);
retVal = timestamp.toString();
break;
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
retVal = rs.getString(col);
break;
case Types.NUMERIC:
case Types.DECIMAL:
java.math.BigDecimal numeric = rs.getBigDecimal(col, 10);
retVal = numeric.toString();
break;
case Types.BIT:
boolean bit = rs.getBoolean(col);
Boolean boolObj = new Boolean(bit);
retVal = boolObj.toString();
break;
case Types.TINYINT:
byte tinyint = rs.getByte(col);
intObj = new Integer(tinyint);
retVal = intObj.toString();
break;
case Types.SMALLINT:
short smallint = rs.getShort(col);
intObj = new Integer(smallint);
retVal = intObj.toString();
break;
case Types.INTEGER:
int integer = rs.getInt(col);
intObj = new Integer(integer);
retVal = intObj.toString();
break;
case Types.BIGINT:
long bigint = rs.getLong(col);
Long longObj = new Long(bigint);
retVal = longObj.toString();
break;
case Types.REAL:
float real = rs.getFloat(col);
Float floatObj = new Float(real);
retVal = floatObj.toString();
break;
case Types.FLOAT:
case Types.DOUBLE:
double longreal = rs.getDouble(col);
Double doubleObj = new Double(longreal);
retVal = doubleObj.toString();
break;
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
byte[] binary = rs.getBytes(col);
retVal = new String(binary);
break;
}
return retVal;
}
|
java.sql.Date和java.sql.Time是完全分开的,分别实现Date和Time的功能,以适应一些DBMS的特殊数据类型。
对于DateTime,你只能使用TimeStamp
对于DateTime,你只能使用TimeStamp
|
用TimeStamp
|
借你的贴问一下:
哪位大大能给出一个将String 转为TimeStamp的实例?
哪位大大能给出一个将String 转为TimeStamp的实例?