• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting the date

 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Simple question I'm sure, I have a servlet that is recording a sale and I want to save the date of the transaction onto my database. How can I get the date and can it be stored as a something simple like a string so I can place it in a bean as well?
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it as simple as (new Date()).toString()? Do you mean to retrieve the current Date and Time of the system without the need to format the style of the date/time? If there is nothing serious, you can just use that.

(new Date()).toString()
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using Java's date utility to get the current date of the system, why dont you consider using auto field in DB to capture the insert time?

Nick
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I just want something simple in this case. I want it formatted like 'Jan 12, 1952'.

I was going to use something like:
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CANADA);

But how can I get the date from this as a string which can be loaded in my saleBean, and eventually loaded into my database??

Much thanks to all those who reply!!
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ko Ko Naing:
Is it as simple as (new Date()).toString()? Do you mean to retrieve the current Date and Time of the system without the need to format the style of the date/time? If there is nothing serious, you can just use that.

(new Date()).toString()


That's an interesting idea, I'm using Microsoft Access for my database, and I'm not sure if it has that capability?
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nicholas Cheung:
Instead of using Java's date utility to get the current date of the system, why dont you consider using auto field in DB to capture the insert time?

Nick


Sorry I meant for that last question to be for Nick.

Thanks again!
 
Stefan Willi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Kwok:

I was going to use something like:
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CANADA);

But how can I get the date from this as a string which can be loaded in my saleBean, and eventually loaded into my database??


You get a String, which represent the date by



But, if you want to store a date in a database, why dont store it as date, instead of a string? If you use a PreparedStatement, you can use the setDate() method and the JDBC-driver do the rest for you.

stefan
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Willi:


But, if you want to store a date in a database, why dont store it as date, instead of a string? If you use a PreparedStatement, you can use the setDate() method and the JDBC-driver do the rest for you.

stefan


Ok wow, that sounds like a great idea. My next question will probably seem obvious, how do I do that?
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Kwok:


That's an interesting idea, I'm using Microsoft Access for my database, and I'm not sure if it has that capability?


If you want the capability as Nick said, you can just use the keyword NOW of MS ACCESS. Then it'll provide you with the current date and time to the newly created records.
 
Stefan Willi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Kwok:

Ok wow, that sounds like a great idea. My next question will probably seem obvious, how do I do that?


Assuming, your db-table is named myTable and the column is named myDate and your Connection Object to the DB is named con


Please note, that the method setDate() needs a java.sql.Date, not a java.util.Date. So you may need to convert your date object.

Stefan
[ April 21, 2005: Message edited by: Stefan Willi ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic