如何在java中以yyyyMMddHHmm格式接收输入? [英] How to receive input as yyyyMMddHHmm format in java?

查看:384
本文介绍了如何在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());
}

在输出中我收到错误


ORA-01843:无效月份。

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) 

示例:

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






更新:(table1.table_key是CHAR(24))


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

你应该给你假设key_value被格式化为yyyyMMddhhmm...(由于显而易见的原因,其他格式不起作用)

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)

其他选项在您的查询转换中key_value到TIMESTAMP

Other option is in your query convert key_value to TIMESTAMP

尝试:(我假设你正在使用oracle db,我做了一些搜索)

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

1)对时间戳进行强制转换

1) Do a "cast" to timestamp

从table1中选择count(*)作为计数器,其中timestamp(table_key)>?

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

2)使用 http: //docs.oracl e.com/cd/B19306_01/server.102/b14200/functions193.htm
我不知道你是否可以这样做:

2)Using http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm . I do not know if you can do something like this:

select table(*)作为来自table1的计数器,其中TO_TIMESTAMP(table_key,'YYYYMMDDHHMMHH24MI') >?

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

这篇关于如何在java中以yyyyMMddHHmm格式接收输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆