File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes How can I format my date for SQL? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How can I format my date for SQL?" Watch "How can I format my date for SQL?" New topic

How can I format my date for SQL?

George Larry
Ranch Hand

Joined: Nov 07, 2001
Posts: 52
This is a follow up from a previous entry...
In a previous entry I was given some help with a Date in a query. I tried the changes in a shell and it worked great. However- I'm still left with formatting my date to something I can use.
Here's what I've got:
String bDate "06/07/02"
Or... that same date converted to a sqlDate.
How can I convert that to the format '07-JUN-2002'?
If this doesn't make sense to you- try looking at my other entry 'Need help with SQL Dates using Oracle'
Thank you.
Shoba Ramachandran
Ranch Hand

Joined: Mar 29, 2001
Posts: 68
Hey George,
Try this...
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
George Larry
Ranch Hand

Joined: Nov 07, 2001
Posts: 52
I tried that but it didn't work. I'm getting the same error- ORA-01861: Leteral does not match format string.
When I just print out my query the date is displayed like this: 'June 7, 2002'
I think I need it to look like this: '06-JUN-02'
Any other ideas?
[ June 17, 2002: Message edited by: George Larry ]
Shivade Sanjay

Joined: Apr 09, 2002
Posts: 6
Hi George,
Try using SimpleDateFormat as follows :
public class dt
public static void main (String args[])
DateFormat dateTimeStamp = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT );
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yy");
System.out.println("sdf2 = " + sdf2.format(new java.util.Date()));
The Formatting Symbols are as follows :
Symbol Meaning Presentation Example
------ ------- ------------ -------
G era designator (Text) AD
y year (Number) 1996
M month in year (Text & Number) July & 07
d day in month (Number) 10
h hour in am/pm (1~12) (Number) 12
H hour in day (0~23) (Number) 0
m minute in hour (Number) 30
s second in minute (Number) 55
S millisecond (Number) 978
E day in week (Text) Tuesday
D day in year (Number) 189
F day of week in month (Number) 2 (2nd Wed in July)
w week in year (Number) 27
W week in month (Number) 2
a am/pm marker (Text) PM
k hour in day (1~24) (Number) 24
K hour in am/pm (0~11) (Number) 0
z time zone (Text) Pacific Standard Time
' escape for text (Delimiter)
'' single quote (Literal) '
I agree. Here's the link:
subject: How can I format my date for SQL?
It's not a secret anymore!