aspose file tools*
The moose likes Beginning Java and the fly likes Getting the date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting the date" Watch "Getting the date" New topic
Author

Getting the date

Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
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

Joined: Jun 08, 2002
Posts: 3178
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()


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
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


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
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

Joined: Mar 31, 2005
Posts: 126
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

Joined: Mar 31, 2005
Posts: 126
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

Joined: Mar 11, 2005
Posts: 47
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

Joined: Mar 31, 2005
Posts: 126
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

Joined: Jun 08, 2002
Posts: 3178
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

Joined: Mar 11, 2005
Posts: 47
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the date