Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sql.date trouble

 
k mull
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a table in my database where one of the entries is a 'Date' in the format 'DD-MMM-YY'. I need to create the date myself and enter it into this table but I dont know how to get it in the correct DD-MMM-YY format.

I can create a new Date() using but i can't get it in the format I need.
Also when I pass this new date into the database it returns an error because it is not of type java.sql.date so it doesn't work that way!

Any help would be much appreciated!
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TryThis puts the wrapper on the date object that should allow JDBC to identify this as an SQL DATE value.

Also, this is probably more appropriate in another forum.
 
Anandh Ramesh
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can try using this method.

Calendar cal = new GregorianCalendar();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);

you can do the necessary modification and insert it, in whichever format, using any separator desired.

this is a bit roundabout, but it works.

-Anandh
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
format doesn't matter while putting into DB. Database could have someother format of its own. so do the same what Paul's said. and format the date while getting it from DB. you can do that by using java.text.SimpleDateFormat class.

and let me say. it is really simple as name describe. what you need to do is just look the docs once.

cheers.
[ December 18, 2004: Message edited by: Adeel Ansari ]
 
Seetesh Hindlekar
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
=================
java.sql.date myDate = new java.sql.date(new java.util.date().getTime());

==============================

Replace date by Date if cut/pasting the above code

What is the syntax if I want to have the date in dd/mm/yyyy format to be inserted in the table column of a database?

Rgds,

Seetesh
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ormat doesn't matter while putting into DB. Database could have someother format of its own. so do the same what Paul's said. and format the date while getting it from DB. you can do that by using java.text.SimpleDateFormat class.


as said earlier.
cheers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic