我分多,所以就多给点……呵呵
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;
}
}
6 个解决方案
#1
java.sql.Date和java.sql.Time是完全分开的,分别实现Date和Time的功能,以适应一些DBMS的特殊数据类型。
对于DateTime,你只能使用TimeStamp
对于DateTime,你只能使用TimeStamp
#2
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;
}
#3
借你的贴问一下:
哪位大大能给出一个将String 转为TimeStamp的实例?
哪位大大能给出一个将String 转为TimeStamp的实例?
#4
这个简单
如下即可
class StringToDate {
public java.sql.Timestamp 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类型
//java.sql.Date date = new java.sql.Date(timeDate.getTime());//sql类型
java.sql.Timestamp time = new java.sql.Timestamp(timeDate.getTime());
return time;
}
}
如下即可
class StringToDate {
public java.sql.Timestamp 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类型
//java.sql.Date date = new java.sql.Date(timeDate.getTime());//sql类型
java.sql.Timestamp time = new java.sql.Timestamp(timeDate.getTime());
return time;
}
}
#5
用TimeStamp
#6
试试看,不会丢的.
public class test {
public static void main(String[] args) {
try {
java.util.Date datea = new java.util.Date();
java.sql.Date dates = new java.sql.Date(datea.getTime());
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);
System.out.println("util date:"+dateFormat.format(datea));
System.out.println("sql date:"+dateFormat.format(dates));
}
catch(Exception e) {
System.out.println(e);
}
}
}
public class test {
public static void main(String[] args) {
try {
java.util.Date datea = new java.util.Date();
java.sql.Date dates = new java.sql.Date(datea.getTime());
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);
System.out.println("util date:"+dateFormat.format(datea));
System.out.println("sql date:"+dateFormat.format(dates));
}
catch(Exception e) {
System.out.println(e);
}
}
}
#1
java.sql.Date和java.sql.Time是完全分开的,分别实现Date和Time的功能,以适应一些DBMS的特殊数据类型。
对于DateTime,你只能使用TimeStamp
对于DateTime,你只能使用TimeStamp
#2
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;
}
#3
借你的贴问一下:
哪位大大能给出一个将String 转为TimeStamp的实例?
哪位大大能给出一个将String 转为TimeStamp的实例?
#4
这个简单
如下即可
class StringToDate {
public java.sql.Timestamp 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类型
//java.sql.Date date = new java.sql.Date(timeDate.getTime());//sql类型
java.sql.Timestamp time = new java.sql.Timestamp(timeDate.getTime());
return time;
}
}
如下即可
class StringToDate {
public java.sql.Timestamp 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类型
//java.sql.Date date = new java.sql.Date(timeDate.getTime());//sql类型
java.sql.Timestamp time = new java.sql.Timestamp(timeDate.getTime());
return time;
}
}
#5
用TimeStamp
#6
试试看,不会丢的.
public class test {
public static void main(String[] args) {
try {
java.util.Date datea = new java.util.Date();
java.sql.Date dates = new java.sql.Date(datea.getTime());
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);
System.out.println("util date:"+dateFormat.format(datea));
System.out.println("sql date:"+dateFormat.format(dates));
}
catch(Exception e) {
System.out.println(e);
}
}
}
public class test {
public static void main(String[] args) {
try {
java.util.Date datea = new java.util.Date();
java.sql.Date dates = new java.sql.Date(datea.getTime());
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);
System.out.println("util date:"+dateFormat.format(datea));
System.out.println("sql date:"+dateFormat.format(dates));
}
catch(Exception e) {
System.out.println(e);
}
}
}