java sql时间todate_Java将日期转换为sql时间戳 (Java convert date to sql timestamp)

2014-10-09 04:54:59

0

I'm trying to convert a date (string) extracted from a csv file, convert it to sql timestamp and upload using prepared statement. What I have is:

String test = "8/10/2014 16:59";

DateFormat fromFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");

fromFormat.setLenient(false);

DateFormat toFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");

toFormat.setLenient(false);

Date date2 = null;

try {

date2 = toFormat.parse(test);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf(date2);

//java.sql.Timestamp sqlDate2 = new java.sql.Timestamp(timestamp);

//sql_statement.setTimestamp(1, ts2);

As you can see my code is messy as I'm trying to solve this problem. I'm always getting an error in eclipse:

java.text.ParseException: Unparseable date: "8/10/2014 16:59"

at java.text.DateFormat.parse(DateFormat.java:357)

at com.syntronic.client.thread.ORCThreadTejasInv.uploadOracleDBOptical(ORCThreadTejasInv.java:555)

at com.syntronic.client.thread.ORCThreadTejasInv.connectOracleDB(ORCThreadTejasInv.java:170)

at com.syntronic.client.thread.ORCThreadTejasInv.retrieveOracleTejas(ORCThreadTejasInv.java:125)

at com.syntronic.client.thread.ORCThreadTejasInv.run(ORCThreadTejasInv.java:84)

at java.lang.Thread.run(Thread.java:745)

I even try using:

SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );

String yourformattedDate = sdf.format(test);

and diff error shows up"

Exception in thread "Thread-6" java.lang.IllegalArgumentException: Cannot format given Object as a Date

at java.text.DateFormat.format(DateFormat.java:301)

at java.text.Format.format(Format.java:157)

at com.syntronic.client.thread.ORCThreadTejasInv.uploadOracleDBOptical(ORCThreadTejasInv.java:562)

at com.syntronic.client.thread.ORCThreadTejasInv.connectOracleDB(ORCThreadTejasInv.java:170)

at com.syntronic.client.thread.ORCThreadTejasInv.retrieveOracleTejas(ORCThreadTejasInv.java:125)

at com.syntronic.client.thread.ORCThreadTejasInv.run(ORCThreadTejasInv.java:84)

at java.lang.Thread.run(Thread.java:745)

Anyone can help on why the date is unparseable? and how to convert it to a proper sql timestamp? thank you

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值