一个转换String为Date的东东,我错在哪了啊?请大家赐教,在线等。。。。。

zuoxxx 2006-05-11 01:45:07
翻了翻以前别人问过的关于时间的问题,我写了上面这个东东,目的是想将一个字符串转换成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不能写入日期/时间类型的字段里啊?
...全文
304 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
software_jianghai 2006-05-12
  • 打赏
  • 举报
回复
private Timestamp timeformat(String time){
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;
}
cyxlsm 2006-05-12
  • 打赏
  • 举报
回复
将文本数据解析成日期对象r

假设我们有一个文本字符串包含了一个格式化了的日期对象, 而我们希望解析这个字符串并从文本日期数据创建一个日期对象. 我们将再次以格式化字符串"MM-dd-yyyy" 调用SimpleDateFormat类, 但是这一次, 我们使用格式化解析而不是生成一个文本日期数据. 我们的例子, 显示在下面, 将解析文本字符串"9-29-2001"并创建一个值为001736000000 的日期对象.

例子程序:

import java.text.SimpleDateFormat;
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());
}
}
}


zuoxxx 2006-05-11
  • 打赏
  • 举报
回复
Mon May 08 11:00:00 GMT 2006 这种时间格式存放不进access数据库里

如何才能保持Mon May 08 11:00:00 GMT 2006为 Data类型并且转化成为2006-05-08 11:00:00这种格式呢?
zuoxxx 2006-05-11
  • 打赏
  • 举报
回复
谢谢楼上的各位
现在已经能正常转换了

但是遇到一个问题啊
按照sole_lodestar()的方法转换时间后成为了这种时间格式Mon May 08 11:00:00 GMT 2006

我如何才能得到的时间格式为2006-05-08 11:00:00呢??
再次麻烦各位了
kaukiyou 2006-05-11
  • 打赏
  • 举报
回复
String到Date是parse,Date 到String是format。
我也常常分不清。
higher530 2006-05-11
  • 打赏
  • 举报
回复
fromat()方法返回的是Date对象," Date ok=format1.format(format.parse(time));" 有问题,format1里面的参数类型不对.
sole_lodestar 2006-05-11
  • 打赏
  • 举报
回复

/**
* 字符串转化成日期类型
* @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");

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧