翻了翻以前别人问过的关于时间的问题,我写了上面这个东东,目的是想将一个字符串转换成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不能写入日期/时间类型的字段里啊?
/**
* 字符串转化成日期类型
* @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;
}