How to receive input as yyyyMMddHHmm format in java?

后端 未结 3 896
天命终不由人
天命终不由人 2021-01-21 07:33
SimpleDateFormat sdf = new SimpleDateFormat(\"yyyyMMddHHmm\");
java.util.Date parsedDate = sdf.parse(\"201212130900\");
java.sql.Timestamp timestamp = new java.sql.Times         


        
相关标签:
3条回答
  • 2021-01-21 07:59

    I have solved the above problem.

    I converted the date to "yyyyMMdd" format and converted the time in "HHmm" format, storing them as 2 different strings.

    0 讨论(0)
  • 2021-01-21 08:13

    In java, to get a formated date, you would do the following:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
    String sdf.format(myDate);
    

    There are loads of options in SimpleDateFormat. I have attached the JavaDoc for you.

    0 讨论(0)
  • 2021-01-21 08:19

    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) > ?"

    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 count(*) as counter from table1 where TO_TIMESTAMP(table_key,'YYYYMMDDHHMMHH24MI') > ?"

    0 讨论(0)
提交回复
热议问题