This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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);
dateFormat.format("06/07/2002");
-Shoba
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
Greenhorn

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) '
 
wood burning stoves
 
subject: How can I format my date for SQL?
 
Similar Threads
Date formatting in java.
how to retreive date
java.sql.Timestamp question
SimpleDateFormat Help
Handling a strange date format