1

Possible Duplicate:
Oracle date pattern to java date pattern
Default JDBC date format when reading date as a string from ResultSet

In my java application, I have a date string coming from db in different-different formats each time 05-Feb-2013, along with the format pattern DD-MON-YYYY.

I need to convert this date string to java.util.Date. Java's default java.text.DateFormat implementation, which is java.text.SimpleDateFormat will not work here as it can not understand oracle database's format pattern. My question here is

1) Is there any other implementation of java.text.DateFormat like java.text.SimpleDateFormat (which is an implementation of DatePattern after all), which can accept oracle db's format pattern and return java.util.Date?

or

2) Is there any utility to draw java simpledateformat pattern from oracle db format pattern?

or

3) Can it be achieved in any other way?

Thanks in advance

Community
  • 1
  • 1
Manohar
  • 653
  • 1
  • 10
  • 15
  • 1
    Why are you getting a *string* at all? Is your field not a date type in the database? Note that `SimpleDateFormat` certainly *will* work if you give it the right pattern... – Jon Skeet Feb 05 '13 at 06:49
  • This is the second such question this evening. http://stackoverflow.com/questions/14700962 Must be a homework assignment. – Jim Garrison Feb 05 '13 at 06:55
  • Is this strange API something that someone else designed and you are stuck with? Can you show us an actual code sample? It really seems odd that you wouldn't be able to let the database driver convert into java date objects like normal. – Affe Feb 05 '13 at 07:00
  • I have an xml comming from db. It is not through jdbc. POKER07-JAN-13Y – Manohar Feb 05 '13 at 07:13
  • 1
    Ick - dates in XML but not in ISO-8601? – Jon Skeet Feb 05 '13 at 07:24
  • How many formats do you need to cope with? Can you just have a map from Oracle pattern to SimpleDateFormat pattern? That would be simpler than writing code to convert automatically. – Jon Skeet Feb 05 '13 at 07:25
  • Please don't [post the same question twice](http://stackoverflow.com/q/14689247/266304); if you don't get useful answers, [improve you existing question](http://stackoverflow.com/faq#bounty). – Alex Poole Feb 05 '13 at 08:43

3 Answers3

0

1) if you have 'datevalue' alogn with 'datepattern' you dont have a problem using simpleDateFormatter

SimpleDateFormat format = new SimpleDateFormat(datePatternfromOracle);

2) //do this for all values format = new SimpleDateFormat(datePatternFromOracle); format.format(dateValueFromOracle);

3) there are multiple ways, but this is straight forward

TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
  • That does not work at all. The assertion he makes in the question that java's dateformat does not understand oracle database format strings is correct. – Affe Feb 05 '13 at 06:58
  • Yes..you are right. I made it clear. SimpleDateFormat can not understand datePatternFromOracle – Manohar Feb 05 '13 at 07:11
0

If your value type is Date then first convert it into String by String DateStr = value.toString(); then use SimpleDateFormat class to format as follow : SimpleDateFormat formatter = new SimpleDateFormat(YourFormat); then use this code to format as your wish formatter.format(DateStr); Hope this will help you ..

Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26
0

This SimpleDateFormat pattern should work for your sample date: dd-MMM-yyyy. You may need to set to locale to English for this to work.

You say that the dates are formatted differently each time. How can you hope to parse something if you don't know what the format is?

jackrabbit
  • 5,525
  • 1
  • 27
  • 38