翻了翻以前别人问过的关于时间的问题,我写了上面这个东东,目的是想将一个字符串转换成date类型的时间,最后存入access数据库的日期/时间类型的字段里private Date timeformat(String time){
SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date ok=format1.format(format.parse(time));
return ok;
}我的东东显示错误是在Date ok=format1.format(format.parse(time))类型不匹配,无法将String转换成Date类型之前我直接使用String数据字符串直接向数据库的日期/时间类型添加出错,显示类型不符合,不知道是不是由于String不能写入日期/时间类型的字段里啊?
SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date ok=format1.format(format.parse(time));
return ok;
}我的东东显示错误是在Date ok=format1.format(format.parse(time))类型不匹配,无法将String转换成Date类型之前我直接使用String数据字符串直接向数据库的日期/时间类型添加出错,显示类型不符合,不知道是不是由于String不能写入日期/时间类型的字段里啊?
解决方案 »
- Class<?> cls = Class.forName("com.code.Test"); 这个?怎么具体化,因为Test类是运行时动态加载的,所以我想不到办
- 求教,求一个数分解为N个素数相加和,有多少种分法
- Jacob写word的问题求助
- 请问一个类在内存中是什么时候产生的,什么时候结束的?一个方法呢?
- 怎样实现KeyHandler的keyReleased方法
- 幼稚问题
- JBuilder7 Enterprise在安装之后能执行程序,但不能调试?
- 哪为知道jbuilder 4.0 or 5.0 ,还有jdk1.4地下在地址? 多谢!
- 送分,多多关照
- rs.next运行不了
- 如何在JAVA GUI上嵌入系统软件,如Word
- 问一个关于java.awt.TextArea的问题,详细内容见内。
/**
* 字符串转化成日期类型
* @param _Date
* @return
* @throws Exception
*/
public static Date StringToDate(String _Date) throws Exception{
try{
return m_sdf.parse(_Date);
}catch(Exception e){
throw(e);
}
}
public static SimpleDateFormat m_sdf = new SimpleDateFormat("yyyy-MM-dd");
我也常常分不清。
现在已经能正常转换了但是遇到一个问题啊
按照sole_lodestar()的方法转换时间后成为了这种时间格式Mon May 08 11:00:00 GMT 2006我如何才能得到的时间格式为2006-05-08 11:00:00呢??
再次麻烦各位了
import java.util.Date;public class DateExample3 { public static void main(String[] args) {
// Create a date formatter that can parse dates of
// the form MM-dd-yyyy.
SimpleDateFormat bartDateFormat =
new SimpleDateFormat("MM-dd-yyyy"); // Create a string containing a text date to be parsed.
String dateStringToParse = "9-29-2001"; try {
// Parse the text version of the date.
// We have to perform the parse method in a
// try-catch construct in case dateStringToParse
// does not contain a date in the format we are expecting.
Date date = bartDateFormat.parse(dateStringToParse); // Now send the parsed date as a long value
// to the system output.
System.out.println(date.getTime());
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
try {
Date ok = format1.parse(time);
Timestamp time1 = new Timestamp(ok.getTime());
System.out.println(" **************" + time1);
return time1;
} catch(ParseException e) {
e.printStackTrace();
}
return null;
}