• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Getting the date

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic