• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

how to get date month year time  RSS feed

Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you kindly tell me how to get the "date, month, year, time of the system".
(instanceof Sidekick)
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.
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.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!