2

i want to parse oracle timestamp (01-MAY-12 01.00.47.000000000 PM) to java.util.Date i used this:

Date dateStart=new SimpleDateFormat("yy-MM-dd HH:mm:ss.S").parse("01-MAY-12 01.00.47.000000000 PM");

but i get this error

java.text.ParseException: Unparseable date: "2012-5-1.13.0. 47. 0"

jmj
  • 237,923
  • 42
  • 401
  • 438
danarj
  • 1,798
  • 7
  • 26
  • 54

3 Answers3

6

You shouldn't have to parse anything. Use one of the ResultSet.getTimestamp() methods, and you'll have a java.sql.Timestamp object directly, which extends java.util.Date.

java.sql.Timestamp ts = myResultSet.getTimestamp( … );

And this will have the additional advantage of being portale across databases and locales.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Or, if using JDBC 4.2 or later, and Java 8 or later, you may be able to retrieve an `java.time.Instant` via the `ResultSet::getObject` method. – Basil Bourque Sep 05 '16 at 21:10
1

"yy-MM-dd"?

"01-MAY-12"

Is your day number really "12" and your year "01"?

And how come your error shows "2012-5-1.13.0. 47. 0", which is presumably a date in yet another format?

David Aldridge
  • 51,479
  • 8
  • 68
  • 96
0

If you are trying to access it using JDBC then as @JB Nizet suggested use getTimestamp() or if you just have String and need to parse to Date then do it by following

Try with following format

01-MAY-12 01.00.47.000000000 PM

yy-MMM-dd hh.mm.ss.SSSSSSSSSS a
jmj
  • 237,923
  • 42
  • 401
  • 438
  • It doesn't work properly for time stamps which have non zero nano seconds i.e `01-MAY-12 01.00.47.999999999` PM – Katona Feb 03 '14 at 11:01