• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

sql.date trouble

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic