Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get date month year time

 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
could you kindly tell me how to get the "date, month, year, time of the system".
thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you create a date (little d) object, it will contain the current system date & time. Then you can query the year, month, day, hours, etc.
Here's the JavaDoc for one date/time class: GregorianCalendar There are also Date classes in java.util and java.sql. They have different query methods.
Let's ask the rest of the gang how to choose which one to use.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which to use depends on what you want to do with it. Mostly you should use java.util.Calendar (of which GregorianCalendar is a subclass, but this should be entirely transparent.) To get the current system date, use:

This will return a Calendar initialized to the current date/time for the Local that you are in (for most in the Western World, this will be an instance of the GregorianCalendar).
Note that you cannot create a new Calendar directly. (new Calendar() is not permitted).
You can do the same thing with a java.util.Date:

This, to, will return the current system time.
The difference between Calendars and Dates is that Dates are conceptually immutable. (They used to be changeable, but this functionality was depricated in Java 1.2, I think) If you need to do date calculations, use Calendars. If you are storing the dates, use Dates (You can convert between the two through the Calendar's .getTime() and .setTime(Date ) methods). Note that java.sql.Date is a subclass of java.util.Date.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic