Solved

Convert String to sql.Date

Posted on 2002-03-24
11
1,190 Views
Last Modified: 2011-09-20
Hello experts,

 I have an object that contains a date that I retrieve before I insert into an Oracle database. I am having problems converting the string to a date that oracle likes.

String bd = cd.getBeginDate(); //get date String
bd now contains a value like: 2001-02-03 00:00:00.0 //This format is always the case!!!

So I made a simpleDateFormat like this:
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");

Now what I would like to do is put this date into a java.sql.Date object and insert it into Oracle.

I go like this:
java.util.Date bDate = fmt.parse(bd);
long temp = bDate.getTime();   //temp now contrains= 2001-02-03
java.sql.Date beginDate = new java.sql.Date( temp );

                         
System.out.println("update CONTRACT set "+
" BEGIN_DATE = '"+beginDate+"', "+
" END_DATE = '24/MAR/2002', "+
" WHERE ID = '"+cd.getId()+"' ");

When running it I get this...

update CONTRACT set BEGIN_DATE = '2001-02-03', END_DATE = '24/MAR/2002',  WHERE ID = '5'

ERROR:
java.sql.SQLException: ORA-01861: literal does not match format string

As you can see one date is a fixed string. I did that because I can see oracle accepts only a special format of date. How can I make this work to insert my date correctly?
0
Comment
Question by:Bekkerus
11 Comments
 

Expert Comment

by:asnmis
Comment Utility
To specify a date format for the class SimpleDateFormat, use "yyyy.MM.dd" rather than "yyyy-mm-dd". In the context of SimpleDateFormat, "mm" indicates minutes rather than the month.

Option 2: use prepared statment instead and use setDate or setTimestamp. former required java.sql.Data and later java.sql.Timestamp.
0
 

Author Comment

by:Bekkerus
Comment Utility
asnmis,

 Can you show the code based on my example?
0
 
LVL 16

Expert Comment

by:Peter Kwan
Comment Utility
Bekkerus,

Why are your BEGIN_DATE and END_DATE format is different? Your BEGIN_DATE format is "yyyy-mm-dd" while your END_DATE format is "dd/MM/yyyy". How do I know which is correct and which is wrong?
0
 

Author Comment

by:Bekkerus
Comment Utility
pkwan,

 Ahh I did that to make it clear to show how oracle dates have to be formatted. It likes this format '24/MAR/2002'. But I thought that java.sql.Date would handle everything for me when I perform an insert or update??? It fails to insert with my beginDate. How can I fix this? I dont want to parse the string character by character unless it is a last resort.

 Hard to image that others have not encountered String to Date conversion issues with Oracle. Hope someone knows how to fix it.
0
 
LVL 2

Accepted Solution

by:
karlinsky earned 50 total points
Comment Utility
Hi
Why don't you use a prepared statement:

// your sql with '?' place holders instead of the parameters
String sql="update CONTRACT set BEGIN_DATE = ?, END_DATE = ?   WHERE ID = ?";
// prepare a statement for the sql
PreparedStatement stmt=connection.prepareStatement(sql);
// convert your dates into Date objects
SimpleDateFormat df=new SimpleDateFormat("dd-MMM-yyyy",Locale.US);
Date beginDate=df.parse("02-Mar-2001");
Date endDate=df.parse("24-Mar-2001");
// bind parameters
stmt.setDate(1,new java.sql.Date(beginDate.getTime());
stmt.setDate(2,new java.sql.Date(endDate.getTime());
stmt.setString(3,cd.getId());
// execute the update
stmt.executeUpdate();

This way the JDBC driver handles all the DB specific formating. You just give it two java.sql.Date objects.
This approach also can improve performance if you use the same statement multiple times with different parameters (different dates).



Hope this helps.

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Expert Comment

by:sv1040
Comment Utility


One other option is to do the following.

update CONTRACT set BEGIN_DATE = TO_DATE('2001-02-03','YYYY-MM-DD'), END_DATE = TO_DATE('24/MAR/2002','DD/MONTH/YYYY')  WHERE ID = '5'


Regards
SV1040
0
 

Author Comment

by:Bekkerus
Comment Utility
Well this is working. I go through a lot of effort to get it into the correct format. Does this look terrible or what?

java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
                         
//begin date
String dateholder = cd.getBeginDate();
java.util.Date bDate = fmt.parse(dateholder);
long temp = bDate.getTime();
java.sql.Date beginDate = new java.sql.Date( temp );

//end date
dateholder = cd.getEndDate();
java.util.Date eDate = fmt.parse(dateholder);
temp = eDate.getTime();
java.sql.Date endDate = new java.sql.Date( temp );

System.out.println("update CONTRACT set "+
" BEGIN_DATE = TO_DATE('"+beginDate+"','YYYY-MM-DD'), "+
" END_DATE = TO_DATE('"+endDate+"','YYYY-MM-DD'), "+
" WHERE ID = '"+cd.getId()+"' ");
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Use placeholders as mentioned a few times above.
0
 

Expert Comment

by:sv1040
Comment Utility
If you can get rid of the milliseconds part from the dates (using substring function), then you need not do so much of formatting. The below query is sufficient.

update CONTRACT set "+
" BEGIN_DATE = TO_DATE('"+ cd.getBeginDate() +"','YYYY-MM-DD HH:MI:SS'), "+
" END_DATE = TO_DATE('"+ cd.getEndDate(); +"','YYYY-MM-DD HH:MI:SS'), "+
" WHERE ID = '"+cd.getId()+"'");

Regards
SV1040
0
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: karlinsky
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
Per recommendation, force-accepted.

Netminder
EE Admin
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now