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

Date and Time

 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a simple question
Can anyone tell me how to get the date and time of the system?
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pete,
when you initialize a Date with an empty constructor (Date date = new Date();) then it is initialized with the current Date and Time on your computer. Is this what you mean?
Rene
[ July 10, 2002: Message edited by: Rene Liebmann ]
 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, sort of. I need to enter the date and time into a database. So i need to be able to return the date and time in a specific format i.e. if the date and time was July 10th 2002 9:45 i would need the:
date : 2002-07-10
time : 09:45:00
the api isn't very clear on how to achieve this. Can you help?
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this link:
http://java.sun.com/j2se/1.3/docs/api/java/text/SimpleDateFormat.html
/Rene
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, SimpleDateFormat could be one solution. Another Possibility is the Calendar (GregorianCalendar). If you create a new GregorianCalendar then it already is initialized with the current time. Then the Calendar object has lots of methods to get what you want. Therefore the API has a good example.
Good luck
 
Mahesh Mamani
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When using the Calendar class, do take care of this functionality regd. month...
Use this URL for more info

MSM
 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used SimpleDateFormat because that's all i needed.
Thanks to both Rene's, your help was much appreciated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic