java yyyymmddhhmm,如何在java中以yyyyMMddHHmm格式接收输入?

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");

java.util.Date parsedDate = sdf.parse("201212130900");

java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());

try

{

PreparedStatement pstmt = connection.prepareStatement(

"select count(*) as counter from table1 where table_key > ?");

pstmt.setTimestamp(1,new java.sql.Timestamp(timestamp.getTime()));

ResultSet rs = pstmt.executeQuery();

while(rs.next()){

System.out.println(rs.getInt( 1 ));

}

connection.close();

}

catch(Exception g){

System.err.println(g.getMessage());

}

In the output I am receiving an error as

ORA-01843: not a valid month.

Can anyone assist to solve the above error ?

解决方案

Your conversion from string to timestap is OK.(I tested and works).

You problem may be in the time zone or something.

Try to use this method instead,

setTimestamp(int parameterIndex, Timestamp x, Calendar cal)

Example:

setTimestamp(1, timestamp , Calendar.getInstance(TimeZone.getTimeZone("UTC")))

UPDATED: (table1.table_key is an CHAR(24))

You should give you date from java in a String, assuming key_value is formatted as "yyyyMMddhhmm"... (for obvious reasons other format will not work)

Other option is in your query convert key_value to TIMESTAMP

Try: (I'm assuming you are working with oracle db, and I do some search)

1) Do a "cast" to timestamp

"select count(*) as counter from table1 where timestamp(table_key) > ?"

"select count(*) as counter from table1 where TO_TIMESTAMP(table_key,'YYYYMMDDHHMMHH24MI') > ?"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值