Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to handle this Date format?

 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using Object array to retrieve data from database.

I am using M$access. What I get back is the SQL date in format "2005-3-1 00.00.00.0". Now how to get a date like 1-March-2005 from this?

Thanks in Advance,

Maki Jav

ps: I will change my database to oracle. Can you please tell what you use to save date in it. In asscess you use #03/01/2005#. I know that you use single quotes for string and nothing for numbers.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maki Jav:
Now how to get a date like 1-March-2005 from this?


Did you try SimpleDateFormat class for it.

SimpleDateFormat Java Doc
[ December 04, 2006: Message edited by: Saif uddin ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ps: I will change my database to oracle. Can you please tell what you use to save date in it. In asscess you use #03/01/2005#. I know that you use single quotes for string and nothing for numbers.

Use a PreparedStatement.
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saif,

I know about that...

My code snippet is here, you can better me in this...
My object array obj[][]...
String date = obj[row][date].toString();

date= date.substring(0,date.indexOf(" ")); // 2005-3-1 00.00.00.0

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");

Date dt = sdf.parse(date);
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");

date = df.format(dt);

This works and its is fast,even for accessing records from database;but can there be a better approach? I might be missing that...

Thanks,

Maki Jav

ps: There might be some error in my code as I am writting it from memory
[ December 04, 2006: Message edited by: Maki Jav ]
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

Sorry to ask this but can you tell me how?

Here is four variables

int mynumber=10000;
String myname="makijav";
String stringDate="3/31/2005";
Date date = new Date();

please put them in database...

Thanks,

Maki Jav
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

Sorry to ask this but can you tell me how?

Here is four variables

int mynumber=10000;
String myname="makijav";
String stringDate="3/31/2005";
Date date = new Date();

please put them in database...

Thanks,

Maki Jav
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Certainly.



My assumption here is that you know the format you get your date String in and you can parse the string into a valid Date to use in the PreparedStatement. Using a PreparedStatement frees you from the work required to avoid SQL syntax problems (escaping quotes etc.) and frees you from relying on a date format.
[ December 04, 2006: Message edited by: Paul Sturrock ]
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means I don't have to worry about the database?
Only set my values like you have told me and retrieve values just the usual way through ResultSet, right?


Maki Jav
[ December 04, 2006: Message edited by: Maki Jav ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. PreparedStatements free you from having to worry about things like date formats.
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the solution I gave in response to Saif for the handling Timestamp
but without using substring as the date I am getting is 2005-3-1 00:00:00.0
So I am using format yyyy-mm-dd hh:mm:ss.S in my SimpleDateFormat object.

One thing more to ask Paul...

Right now I am using something like

String query="Insert into votesTable (questionID,question) VALUES('"+qid+"','"+ques+"')";

and my next query might be totally different for another table. So this means that if I am to use PreparedStatement, I have to have a class for each table. Do you know about some class generating tool from table that uses PreparedStatement inwardly. I hear that Jenny, available on this site, is one such tool...

Thanks,

Maki Jav
[ December 06, 2006: Message edited by: Maki Jav ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So this means that if I am to use PreparedStatement, I have to have a class for each table

Not sure I follow this. You will probably need an instance of PreparedStatement for each insert statement (though obviously you can resuse this by changing the values of the bound parameters). Can you explain what you mean?
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, if my English did not make sense. What I meant was that I have various tables and these tables have fields which are unique to that table.

table1 has date and nameID fields
table2 has nameID,name,address and phone fields.

Now I cannot use the same prepared statement for both, right?
I better have two classes (or methods in one class), such as:



Maybe, I am clear now

Besides, if I am not mistaken, I have seen PreparedStatement being used for delete statements. Am I right? if so, can you please give me an example...

Oh I get it!
It must be
String statementSQL = "DELETE from table2 where nameID=? ; ";

and for update
String statementSQL = "UPDATE table2 set name=?, address=? , phone=? where nameID=?; ";

Right?!

Thanks,

Maki Jav
[ December 06, 2006: Message edited by: Maki Jav ]
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think I am having this problem....

Using the code posted for date formating, I input date 2005-4-02 but I am retrieving 02-Jan-2005, What could be the reason?

Maki Jav
 
Subhadip Chatterjee
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mak,
did you try printing the Date after parsing the DB output-string?...and By the way, i would suggest you type cast the Date object into java.sql.Date itself (if your driver is at all mapping the DB-field with oracle.sql.Date).My point is, don't need to do any toString() on that, and then directly format the Date. So, you are reducing the "substring" and "parsing to date" effort. and please try to print the DB-date_field value before formatting...as a doubly you can store the resultset date-value into an Object reference and print reference.class, to know what java type, your version of JDBC driver is mapping the DB-date field into !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic